Jump to content

Can I get a little help with pattern matching a url?


Prismatic

Recommended Posts

Im trying to make a bit of markup code that allows users to post images on my forums, ive come up with this sofar..

[code]
if (strstr($text, "[image")){
                    $text = ereg_replace("\[image=([\#0-9a-zA-Z]*)\]", "<a class='BoardRowBLink' target='_blank' href='\\1'><img src='\\1' border='1' height='120' hspace='5' vspace='5' width='160'></a>", $text);
                }
[/code]

It's supposed to find stuff like this => "[image=http://www.somesite.com/blah.jpg/gif/png/bmp]" and transform it into a picture but it's not matching at all :(


Any ideas?
Link to comment
Share on other sites

It's a forward slash to look for not a backslash. You forgot the period, and colon too. I like preg_replace. Try:

[code]
$text = preg_replace('/\[image *= *([\/#:.0-9a-zA-Z]*?)\]/i', "<a class='BoardRowBLink' target='_blank' href='$1'><img src='$1' border='1' height='120' hspace='5' vspace='5' width='160'></a>", $text);
[/code]

I added a space and asterisk around the equal sign in case people put one or more spaces before specifying the url. Take it out if you don't want to allow that. The question mark after the last asterisk just changes it from greedy to lazy capture.

hth.
Link to comment
Share on other sites

[!--quoteo(post=359492:date=Mar 28 2006, 07:47 PM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ Mar 28 2006, 07:47 PM) [snapback]359492[/snapback][/div][div class=\'quotemain\'][!--quotec--]
It's a forward slash to look for not a backslash. You forgot the period, and colon too. I like preg_replace. Try:

[code]
$text = preg_replace('/\[image *= *([\/#:.0-9a-zA-Z]*?)\]/i', "<a class='BoardRowBLink' target='_blank' href='$1'><img src='$1' border='1' height='120' hspace='5' vspace='5' width='160'></a>", $text);
[/code]

I added a space and asterisk around the equal sign in case people put one or more spaces before specifying the url. Take it out if you don't want to allow that. The question mark after the last asterisk just changes it from greedy to lazy capture.

hth.
[/quote]

Thanks for the help toplay but it does not appear to be matching. It's hitting the if statment fine, i tested with some echo's

[code]
if(in_array("IMG", $PermissionsArr)){
        echo "PERMISSION HIT";
        if (strstr($text, "[image")){
            echo "MATCHED IMAGE TAG";
            $text = preg_replace('/\[image *= *([\/#:.0-9a-zA-Z]*?)\]/i', "<a class='BoardRowBLink' target='_blank' href='$1'><img src='$1' border='1' height='120' hspace='5' vspace='5' width='160'></a>", $text);
        }
    }
[/code]

Both echo's fire so it's seeing the tag, but it's not matching it. The tag I tested with is as follows:

[code]
[image=http://www.phpfreaks.com/images/logo_blank.gif]
[/code]

(Not leeching BW php Freaks, needed a quick image to test with =P )

It's not seeing that apparently and it's showing up as is =/ Any ideas?
Link to comment
Share on other sites

Just play around with it. It depends how easy or tight you want to make the match. You can get away with simply using:

/\[image *= *(.+)\]/i

There's also other characters that can be allowed in a URL such as dash (-) and underscore (_) which you don't have specified right now. Example:
[code]
<?PHP

$text = '[image=http://www.phpfreaks.com/images/logo_blank.gif]';

$text = preg_replace('/\[image *= *(https?:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])\]/i', "<a class='BoardRowBLink' target='_blank' href='$1'><img src='$1' border='1' height='120' hspace='5' vspace='5' width='160'></a>", $text);

echo htmlentities($text);

?>
[/code]

The above code displays:
[!--html--][div class=\'htmltop\']HTML[/div][div class=\'htmlmain\'][!--html1--]<[color=blue]a class[/color]='[color=orange]BoardRowBLink[/color]' target='[color=orange]_blank[/color]' href='[color=orange]http://www.phpfreaks.com/images/logo_blank.gif[/color]'><[color=blue]img src[/color]='[color=orange]http://www.phpfreaks.com/images/logo_blank.gif[/color]' border='[color=orange]1[/color]' height='[color=orange]120[/color]' hspace='[color=orange]5[/color]' vspace='[color=orange]5[/color]' width='[color=orange]160[/color]'><[color=blue]/a[/color]>
[!--html2--][/div][!--html3--]

So, experiment and see what works for you. You can also find help by searching for free expressions at:

[a href=\"http://regexlib.net\" target=\"_blank\"]http://regexlib.net[/a]

Good luck.
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.