SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
Thread: Parsing string data with PHP
-
May 6, 2009, 12:54 #1
- Join Date
- Nov 2002
- Location
- Maryland
- Posts
- 307
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Parsing string data with PHP
I would like to create a function that looks for a name of an attorney within a string and if it finds the name then the function will automatically link it.
I think the following example makes it clear what I am trying to do.
PHP Code:$attorney_array = array(1=>'Michael Anderson', 2=>'Tom A. Atkinson', 3=>'Pat M. Boyer');
$text = '<p>Pat M. Boyer started her career as an audiologist in 1981, and has worked in that capacity in major medical centers in the Cleveland area as well as at the university level.</p>';
function parse_attorney_bio($text, $attorney_array){
}
echo $text = parse_attorney_bio($text, $attorney_array);
// Output
<p><a href="bio.php?id=3">Pat M. Boyer</a> started her career as an audiologist in 1981, and has worked in that capacity in major medical centers in the Cleveland area as well as at the university level.</p>';
Thanks!
-
May 6, 2009, 13:20 #2
- Join Date
- Apr 2008
- Location
- North-East, UK.
- Posts
- 6,111
- Mentioned
- 3 Post(s)
- Tagged
- 0 Thread(s)
Here you go, you could quite easily create a function for this...
PHP Code:<?php
$aAttorneys = array(
1 => 'Michael Anderson',
2 => 'Tom A. Atkinson',
3 => 'Pat M. Boyer'
);
$sString = 'We have top attorneys such as, Michael Anderson, Tom A. Atkinson & Pat M. Boyer in our employ.';
foreach($aAttorneys as $iId => $sAttorney)
{
$sString = str_replace(
$sAttorney,
sprintf(
'<a href="bio.php?id=%s">%s</a>',
$iId,
$sAttorney
),
$sString
);
}
echo $sString;
#We have top attorneys such as, <a href="bio.php?id=1">Michael Anderson</a>, <a href="bio.php?id=2">Tom A. Atkinson</a> & <a href="bio.php?id=3">Pat M. Boyer</a> in our employ.
?>@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
-
May 6, 2009, 13:27 #3
- Join Date
- Nov 2002
- Location
- Maryland
- Posts
- 307
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Awesome SilverBulletUK! You are good. Thank you very much.
Bookmarks