Results 1 to 7 of 7

Thread: String Replace Help in PHP

  1. #1
    Gimme Fries with that!
    Join Date
    Aug 2004
    Posts
    1,469

    String Replace Help in PHP

    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

    PHP Code:
    $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); 

  2. #2
    Administrator Chris's Avatar
    Join Date
    Feb 2003
    Location
    East Lansing, MI USA
    Posts
    7,055
    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?
    Chris Beasley - My Guide to Building a Successful Website[size=1]
    Content Sites: ABCDFGHIJKLMNOP|Forums: ABCD EF|Ecommerce: Swords Knives

  3. #3
    Gimme Fries with that!
    Join Date
    Aug 2004
    Posts
    1,469
    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

  4. #4
    Administrator Chris's Avatar
    Join Date
    Feb 2003
    Location
    East Lansing, MI USA
    Posts
    7,055
    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.
    Chris Beasley - My Guide to Building a Successful Website[size=1]
    Content Sites: ABCDFGHIJKLMNOP|Forums: ABCD EF|Ecommerce: Swords Knives

  5. #5
    Sean
    Join Date
    Nov 2005
    Posts
    139
    Quote Originally Posted by Chris
    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):

    Code:
    <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.

    1. Find the word/phrase that could potentially become a link
    2. Check for the next instance of "</a>" after the word/phrase in the content
    3. If there is no instance of "</a>" after it, it's not a link.
    4. 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
    5. 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

  6. #6
    Registered
    Join Date
    Apr 2006
    Location
    Michigan
    Posts
    99
    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:
    Code:
    <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.

    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.

  7. #7
    Gimme Fries with that!
    Join Date
    Aug 2004
    Posts
    1,469
    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.

Similar Threads

  1. Review: Professional PHP Programming
    By Chris in forum Books
    Replies: 6
    Last Post: 07-17-2013, 05:26 AM
  2. Replies: 0
    Last Post: 08-03-2005, 09:35 AM
  3. Replies: 8
    Last Post: 11-12-2004, 04:10 PM
  4. Replies: 0
    Last Post: 10-29-2004, 09:43 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •