SitePoint Sponsor |
|
User Tag List
Results 1 to 1 of 1
-
Feb 15, 2007, 15:40 #1
- Join Date
- Jan 2007
- Location
- Orlando, FL
- Posts
- 417
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
scanning a longstring for matches in a database
Okay, so I have some whole pages that are stored in mysql as longtext. News articles, comments, that sort of thing.
I also have a table with names that I would like to make links whenever they come up in this text.
for discussion sake, lets get some names.
'$newsarticle' is my display text, I want to add the links to this.
'people' is a database with a name field that I want to match up and create links in $newsarticle. It also has an id field that will be needed to link to the data in this 'people' database.
Would this be way too slow? (syntax probably wrong)
Code:$sql="select name, id from people"; $result=mysql_query($sql); while($row=mysql_fetch_array($result)){ $name=$row['name']; $id=$row['id']; $offset=0 while($match===strpos($newsarticle,$name,$offset)){ //some magic code to insert anchor tag before and after //the $name occurance and then reset the offset to after the first match } }
Which brings me to the point of whether using strreplace would be better.
I notice that you can use an array for the first and second values of the strreplace. If the first array was a list of all the names, and the second was an array with all the html links that would be pretty nice.
How do you go from:
$sql="select name, id from people";
$result=mysql_query($sql);
to an array filled with all the returned names, and another array filled with all the returned names but with link data containing that id something like "<a href=/personal-info/id>name</a>" I know I could do it in a loop, but is there some faster with with an explode or implode and some fixed formatting?
My unease here is that I don't really understand what $result is. Is it just simply an array of associative arrays? Or is the fact that it is a resource mean it has to be handled specially. Can I go into $result and just grab the id number from the third returned result (index 2 I assume)?
Bookmarks