PDA

View Full Version : String Replace Help in PHP



Emancipator
07-20-2006, 03:02 PM
I am sure KLB or one of you other php gurus can help me with this. I am trying to do a string replace the catch is i do not want to replace text within an url. Here is my existing code and if anyone can help me with code that would NOT replace withing an url you will be my hero :)


$cast = $row_cat_movie['cast'];
$st_str = trim($cast, ' '); // Trim white spaces
$strs = explode(',', $cast); // Explode into an array by delimiter
$content = $row_movie_news['desc']; //the text to apply it to.

for($i=0; $i<sizeof($strs); $i++)
{
$strs[$i] = trim($strs[$i], ' ');
$content = str_replace($strs[$i],'<a href="celeb-'.str_replace(" ", "-", $strs[$i]).'.htm"><font color="#666666">'.$strs[$i] .'</font></a>',$content);

}
echo cleanupSmartQuotes($content);

Chris
07-20-2006, 04:09 PM
You know I've always considered you one of the PHP gurus of this forum...

Anyways, would I be correct in assuming you are trying to automatically annotate movie news with links to bio pages for various stars?

When you say you don't want it in a URL, do you mean within an actual URL, or within the anchor text of a link?

Emancipator
07-20-2006, 04:16 PM
you are exactly right you can see how i am using the code here in an interview with Johnny Depp http://www.moviesonline.ca/movienews_9211.html

sometimes i might use Johnny's name as a link and I would like to not have my handy keyword gizmo screwing up a link.

I like to think i know alot about php but am smart enough to realize I am still learning :)

Chris
07-20-2006, 06:48 PM
Well... what if you required there to be a space on either side of the name to do the replacement? Or a space at the beginning but either a space, comma, or period, at the end? That way if there are any other funky characters already surrouding the name (such as > <) the replacement won't happen.

Sean
07-20-2006, 08:13 PM
Well... what if you required there to be a space on either side of the name to do the replacement? Or a space at the beginning but either a space, comma, or period, at the end? That way if there are any other funky characters already surrouding the name (such as > <) the replacement won't happen.

The problem with that is that the person's name could be inside the anchor text like (just an example):


<a>Check out this interview with Johnny Depp at xyz.com</a>

Johnny Depp would be linkified.

I'm no PHP guru, but this is what I got:

I'm not sure how to code this off my head, but I do think it can be done easily if you know the functions.


Find the word/phrase that could potentially become a link
Check for the next instance of "</a>" after the word/phrase in the content
If there is no instance of "</a>" after it, it's not a link.
If there is, take the text in between the word/phrase and the "</a>" and check if it contains "<a href" or "<a " or just something to identify a beginning of a link
If it doesn't, you know it is a link, if it does, you know it isn't.


As far as I can tell this, or the opposite (finding the beginning of a link and then the end), is really the only way to do this, there might be a more simple way of coding it though.

HTH

Selkirk
07-20-2006, 08:30 PM
I assume $row_movie_news['desc'] contains HTML?

The only way to do this in a manner that is both foolproof and general is to parse the HTML into its constituent parts, do the replacement in the relevant place (the character data sections), and then re-assemble the HTML.

For example, you don't want to make the substitution for "cast" in a tag like:


<img src="..." alt="cast hamming it up"> or <a href="http://www.cast.com/">

How you might do this depends on whether you can count on $row_movie_news['desc'] being 100% valid XML.

There are a variety of methods for parsing the XML/HTML in PHP. The SAX based XML parser, Simple XML, or the XML DOM. Additionally, there are some native PHP libraries that can parse XML or even HTML, such as PEAR's XML_HTMLSAX or SAXY.

I've written a parser which is pretty good at dealing with both HTML, XML and doesn't get too confused about malformed input. It hasn't been formally released, but is available here (http://svn.sourceforge.net/viewcvs.cgi/wact/trunk/wact/xml/sax/liberal.inc.php?view=markup&rev=77).

Additionally the php function preg_replace has a word boundary based match, which can help to avoid (unlikely?) problems with a cast member's name being a subset of some other word. This could be used if $row_movie_news['desc'] is not HTML, or when doing the replacement in the character data sections of the HTML.

Alternatively, you might be able to come up the a regular expression for matching only the character data sections of the HTML, but there would be more chance that the regular expression could produce undesired results than if you use a dedicated HTML parser. Much depends on how well you can trust your input.

Emancipator
07-21-2006, 08:36 AM
can you give me a sample usage for your function in my application? Thanks! I knew we would have some php gurus on the web-pub.