SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    823
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question PHP String Function to Replace Text

    This is kind of a quirky request:
    I have dynamic data from a database (all text). I'd like to know what PHP syntax would I need to implement a search of a string and add a link to it?

    For example, if I have the text: Fire Department Staff in the source code, I want to replace it with the following in the source code: <a href="http://www.firedepartment.com" target="blank">Fire Department Staff</a>

    I have about 50 text phrases that I need to apply a variety of links to. Would I need to use substr_replace, ereg_replace, str_replace, or? I've been to php.net and have seen many examples, but I'm not sure which function would be most efficient.

    Also, am I able to create a variety of the needed replacements in a .php file and then import it before my <!DOCTYPE...> declaration? Am I asking the impossible?

    toad78

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You would need to know the text to "transform", and the url, presumably from a database.

    table: links
    ========
    url | www.firedepartment.com
    title | Fire Department Staff

    anyhow you'd likely end up with an array of links looking like this;

    $row[0]
    $row['url']
    $row['title']

    so you need to go through your text file, when you find the match, do the transform;
    PHP Code:
    $txt " This is where the Fire Department Staff are at";

    $rows[0]['title'] = "Fire Department Staff" // your db results set
    $rows[0]['url'] = "www.firedepartment.com" ;

    foreach( 
    $rows as $row ){

    $txt str_replace$row['title'
                            , 
    "<a href=http://" $row['url'] . ">" $row['title'] . "</a>"
                            
    $txt ) ;
    }

    echo 
    $txt 
    Gives:
    This is where the <a href=http://www.firedepartment.com>Fire Department Staff</a> are at

    Simple but effective.

    The things you need to consider eventually will be;

    How do I help users who make an errror? e.g.

    Fire Department staff (lcase s in staff)
    Fire Department Staff (2 spaces before Staff)
    How do I communicate which are the correct spellings of magic phrases?

    Can I make a drag/drop list so that users can just pick phrases and drop them onto a wysiwyg editor. Let me know when you have that last one sorted out

    You can get more complex with this of course, but the big thing to tackle is probably this one. Where do I do this transform?

    Before I store it in the dbase?
    When I get it out of the dbase?

    Or, consider store two versions of the text, the raw one without the transformations, so that the text is easily editable, and the transformed one, ready to be extracted and displayed without having to do all the str_replace stuff every time someone requests a page.

    OR EVEN create a cached (html) version of the transformed text every time the admin users saves a new copy.

    Good luck with it.

  3. #3
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    823
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Red face

    Currently this is what I have:

    PHP Code:
    <?php
    mysql_select_db
    ($database_m18ijdklim$m18ijdklim);
    $query_getContent "SELECT home.content FROM home";
    $getContent mysql_query($query_getContent$m18ijdklim) or die(mysql_error());
    $row_getContent mysql_fetch_assoc($getContent);
    $totalRows_getContent mysql_num_rows($getContent);
    ?>
    HTML Code:
    <?php
    $text = nl2br($row_getContent['content']);
    $after = str_replace("<br />", "</p><p class=\"bod\">", $text); ?>
    <p class="bod"><?php echo $after ; ?></p>
    At this point, I'm unsure of how to implement your solution to scan the HTML text to give me a link.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I am unsure of what your question is.

    Show a very short sample of some text you have, and some text you want to end up with.

  5. #5
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    823
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Red face

    Currently the page says:

    "Come for a Wine Tour with us."

    What I would like for PHP to do is take 'Wine Tour' text and change it to a link 'Wine Tour' with the <a href...> in the source code.

    toad78

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    $txt " Come for a Wine Tour with us";

    $rows[0]['title'] = "Wine Tour" // your db results set
    $rows[0]['url'] = "www.our-tours.com/wine" ;

    foreach( 
    $rows as $row ){

    $txt str_replace$row['title'
                            , 
    "<a href=http://" $row['url'] . ">" $row['title'] . "</a>"
                            
    $txt ) ;
    }

    echo 
    $txt 

    The trick is to get the title and url of the links from your database as an array.
    Code:
    ====
    links
    ====
    id
    title
    url
    
    //example content for table links:
    
    1
    Wine Tours
    www.our-tours.com/wine
    
    2
    Wine Tasting
    www.our-tours.com/tasting.php
    Then as long as your results are accessible as an array, its all automated for you.

    $rows = "select title, url from links" ;


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
  •