Jump to content


Photo

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


  • Please log in to reply
4 replies to this topic

#1 Prismatic

Prismatic
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego

Posted 28 March 2006 - 12:02 PM

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

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);
                }

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?

#2 Prismatic

Prismatic
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego

Posted 28 March 2006 - 10:40 PM

Bump :)

#3 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 29 March 2006 - 12:47 AM

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

$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);

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.

#4 Prismatic

Prismatic
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego

Posted 29 March 2006 - 10:23 AM

[!--quoteo(post=359492:date=Mar 28 2006, 07:47 PM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ Mar 28 2006, 07:47 PM) View Post[/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:

$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);

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

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);
        }
    }

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

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

(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?

#5 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 29 March 2006 - 05:28 PM

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:
<?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);

?>

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users