SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    276
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_match to strip link from content

    Hi.

    I am using this bit of code to identify certain links within my content and swap them for another link.

    PHP Code:
    $re1='(\\[url\\])';    # Square Braces 1
    $re2='.*';    # Non-greedy match on filler
    $re3='(\\[\\/url\\])';
    $articleContent .=preg_replace("/".$re1.$re2.$re3."/i"'<a href="http://www.lesson-ology.com/content.php?whereto=416">Sign Up For Access</a>'$resource[1]); 
    The problem with it is that it replaces everything between the tags what I want to do is replace ONLY the url and leave the rest intact.

    What would the reg_ex look like to accomplish this?

    Thanks
    Chris

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    the slashes make my head hurt.

    So you want to take anything between
    PHP Code:
    [url]Somestuffgoeshere[/url
    and make it into $resource[1]? Cause... your example there doesnt use the [url] tags...

    EDIT: lol. go go BBCode translator. Wrapped in PHP tags to prevent translation.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    Bah. Got the parameters backwards in my head. Okay so you want to take anything between square bracketed URL tags and replace it with a fixed string....
    What exactly did you want to 'save' from the original string? Display it where? Give me an example of your input, followed by what you want it to look like when you're done.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  4. #4
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    276
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    Thanks for replying. It is just standard formatted text (as supplied by ckeditor)

    An example of this could be...

    HTML Code:
    <span style="font-size: 14px;"><span style="color: rgb(0, 102, 255);"><span style="font-family: verdana,geneva,sans-serif;"><strong>Heart to Heart: Building Connections With Hard-to-Reach Students</strong></span></span></span></p>
    <p>
    	<span style="color: rgb(178, 34, 34);"><strong><span style="font-size: 14px;"><span style="font-family: verdana,geneva,sans-serif;">Replay pack Available:</span></span></strong></span>[url]<a href="https://www.example.com/" target="_blank"><span style="font-size: 14px;"><span style="font-family: verdana,geneva,sans-serif;"><strong>Now</strong></span></span></a>[/url]</p>
    <p>
    I need to grab the url from within a link that is surrounded by the url/url and replace that url with another. Now I need to identify the link by url/url because there are some links on the page that do not require them to be changed.


    I hope that is enough to go on.

    Thanks for helping
    Chris

  5. #5
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You would need to use a backreference, in order to retrieve values from the original string.

    So if you have

    PHP Code:
    [url]<a href='http://google.com'>Google</a>[/url
    You would write it as (note: i didn't test this, just for illustration)

    PHP Code:
        $url preg_replace("~\[url\]<a.*?>(.*?)</a>\[\/url\]~""<a href='http://yahoo.com'>\\1</a>"$string); 
    Also, "non-greedy" requires a ? after .*

  6. #6
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    276
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Wonshikee.

    Thanks so much for that, It worked perfectly out of the bag!

    Chris

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    side note: The \ infront of the / is unneeded because you're using ~ as a delimiter, instead of /.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


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
  •