Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2007
    Posts
    273

    [How to] Exclude replacement inside hyperlinks, maybe using if statement,or other way

    Hi guys, below are simple script for word replacement, it is replacing a word in to link



    PHP Code:
    $word = array(
    'google',
    'yahoo'
    );

    $link = array(
    '<a href="http://google.com">google</a>',
    '<a href="http://yahoo.com">yahoo</a>'
    );


    $this->post['message'] = str_ireplace($word$link$this->post['message']); 






    for example text
    i love google result
    the output printed will be like this
    HTML Code:
    i love <a href="http://google.com">google</a> result
    it is just replacing google text into a link like this <a href="http://google.com">google</a>






    but the problem is if the original google text is already inside a link, like this
    HTML Code:
    <a href="http://google.com">i love google result</a>
    the output printed will be broken like this, there will be double hyperlink
    HTML Code:
    <a href="http://google.com">i love <a href="http://google.com">google</a> result</a>





    please help guys, how to exclude the text google inside hyperlinks above from replaced, but the text google outside hyperlinks are still replaced

    maybe using if statement in the simple script above, or other way
    Lets be stable diligent person

  2. #2
    Join Date
    Mar 2011
    Posts
    41
    try changing:
    $word = array(
    'google',
    'yahoo'
    );

    to:
    $word = array(
    ' google ',
    ' yahoo '
    );

    I would think this would work by assuming that the only words you want to replace have spaces before and after.

  3. #3
    Join Date
    Jul 2007
    Posts
    273
    Quote Originally Posted by nafirici View Post
    try changing:
    $word = array(
    'google',
    'yahoo'
    );

    to:
    $word = array(
    ' google ',
    ' yahoo '
    );

    I would think this would work by assuming that the only words you want to replace have spaces before and after.
    i already tried that, but output printed still same

    becaue its still same have space in the front and in the end, inside a hyperlink or not, like this in red

    <a href="http://google.com">i love google result</a>




    please help guys to solve this
    Lets be stable diligent person

  4. #4
    Join Date
    Jul 2009
    Location
    Indiana
    Posts
    2,193
    You'll probably need to use an HTML parser for this. You might be able to get by with a regex but I'd go for a full HTML parser.
    Sam Barrow - CEO @ SQUIDIX (1-855-SQUIDIX)
    Ask Us About Sponsoring Your Web Site (High Traffic Sites Only)
    Squidix - Shared, Reseller, Semi-Dedicated, Managed VPS and Managed Dedicated Hosting
    Midwestern Web - Web Design & Development Services

  5. #5
    Join Date
    Mar 2011
    Posts
    41
    that's surprising to me, it looks to me like you're replacing the word "google" with the hard coded link of "<a href='http://www.google.com'>google</a>". I'll play around with this a bit and see if I can get it to work.

  6. #6
    Join Date
    Apr 2008
    Posts
    306
    A regex might be able to cover most situations :

    PHP Code:
    <?php

    $search 
    'Google';
    $pattern "/( )?(<(.)*>)?( )*{$search}( )*(<(.)*>)?( )?/i";
    $replacement " <a href='http://google.com'>google</a> ";

    $subject 'I love Google result.';

    echo 
    preg_replace($pattern$replacement$subject) . '<br/>'// I love <a href='http://google.com'>google</a> result.<br/>

    $subject 'I love <a something>  GoOgle<a something> result.';

    echo 
    preg_replace($pattern$replacement$subject); // I love <a href='http://google.com'>google</a> result.
    But a regex alone couldn't safely fully cover your problem.
    Last edited by WootWoot; 04-19-2011 at 10:07 PM.
    Heymman - Beefy servers, tiny price !

  7. #7
    you can try Datacol to parse what you need without regexes

Similar Threads

  1. Preventing outward hyperlinks
    By badhat in forum Hosting Security and Technology
    Replies: 0
    Last Post: 09-05-2006, 02:38 PM
  2. Visited Hyperlinks-> Different Color?
    By seodevhead in forum Web Design and Content
    Replies: 4
    Last Post: 12-09-2005, 03:38 PM
  3. Need help with Flash Hyperlinks
    By GifGraphix in forum Design Offers
    Replies: 2
    Last Post: 12-22-2003, 12:59 AM
  4. unwanted hyperlinks
    By John Wise in forum Programming Discussion
    Replies: 4
    Last Post: 06-01-2003, 11:32 AM
  5. Hyperlinks
    By Helpme.co.uk in forum Hosting Security and Technology
    Replies: 0
    Last Post: 09-05-2001, 06:36 AM

Posting Permissions

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