Jump to content

Looking for some help to not include something in preg_replace


brunelle25

Recommended Posts

What I'm trying to do is replace message_tags to links and web addresses to links in facebook json data, the problem is I need to do these in 2 steps, first do the message tags then then web addresses. The message_tags need to be done first because they rely on offset and length of the message to do the replace.

 

 

To code to first replace the message_tags is

// convert message tags to links
if($post->message_tags) {
     usort($post->message_tags, 'mySort');
     foreach($post->message_tags as $message_tag) {
          foreach($message_tag as $link) {
               $post_text = substr_replace($post_text,"<a href=\"https://www.facebook.com/".$link->id."\" target=\"_blank\">".$link->name."</a>",$link->offset,$link->length);
          }
     }
}

The second part is to convert any web address (http://example.com) to a link (<a href ="http://example.com" title="http://example.com" target="_blank">http://example.com</a>)

// check if any entites exist and if so, replace them with hyperlinked versions
$post_text = preg_replace("/((http:\/\/|https:\/\/)[^ )]+)/e", "'<a href=\"$1\" title=\"$1\" target=\"_blank\">'. ((strlen('$1')>=250 ? substr('$1',0,250).'...':'$1')).'</a>'", $post_text);

So now to the problem, the second part now rewrites the first part and completely destroys the link because it just looks for http: and https: so in my preg_replace I need a way to not include anything that has already been converted, so not include anything that already has href="http

Link to comment
Share on other sites

if the message tags does not contain http:// links then replace urls in $post_text before replacing the message tags.

 

Also you should not use e pattern modifier for you replacement string. This could allow a hacker to run malicious code. It is is better to use preg_replace_callback. Your regex converted below

// for each match this function is called. It will wrap the url in HTML anchor tags
function url_to_link($matches)
{
    $url = $matches[0];
    
    $link  = "<a href=\"$url\" title=\"$url\" target=\"_blank\">";
    $link .= strlen($url) >=250 ? substr($url, 0, 250) . '...' : $url;
    $link .= '</a>';

    // return the HTML for the link
    return $link;
}

$post_text = preg_replace_callback("/(https?:\/\/[^\s]+)/", 'url_to_link', $post_text);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.