Page 1 of 1

Function returns a string in ereg_replace()

Posted: Tue Jul 15, 2003 10:52 am
by delorian
Hi,
Let's say that I want to create a html tag from the url parsed to script from a textarea. The url could point on www page -> like http://www.wp.pl , attachement -> http://www.somesite.com/attachment.zip or img -> http://www.somesite.com/picture.jpg. Depending on what url points on the function should return typical '<a href' or '<img src' stuff. I wrote the function and it's doing great so there's no problem with that.

The problem is how to parse the url to that function from ereg_replace() :?:

I've tried the following:

Code: Select all

$html_tag = ereg_replace("[[]]+://[^<>[]]+[[]/]", 
                    create_tag_from_url("\\0"), $textarea_content);
But it's obvious that it doesn't work properly. Could you tell me why how to correct this (if that's possible) or what I should read to learn more about this. (I've looked in the php.net - but maybe not good enough)

I would be very gratefull.

Posted: Tue Jul 15, 2003 1:53 pm
by m3rajk
first off use preg instead of ereg.
ereg is posix. posix is ALWAYS greedy
preg is perl, and in this case the non-greedy ability of perl is a benefit


personally i would ddo it in multiple passes.
pass one: anything starting with http and ending with the image tags you want to pick up get switched

pass 2: the rest get switched

so pass one is something like preg_replace('|(http://[\w\.\-]+)|i', '<img src="\1" border="0" target="_blank">', $input);

and i'm sure from what you posted i didn't need to give you that much, so since i've pointed you in a direction that can work i'll stop now so you can get it on your own and learn it

Posted: Wed Jul 16, 2003 3:15 am
by delorian
Ok, I've managed to do it :D First of all I must say that I don't like regulars. I know that they're very powefull tool, and I'm usign it for a long time, but I often had problems with them. I've read about PRCE and you were right, it's better than POSIX. The second thing is that I am going to buy O'R's Mastering Regular Expressions today - "if you don't like something (and you must use it) it's about time to learn it." :D

Ok, and there's the code: (if you find some error...)

Code: Select all

function url_create($url_sContent) {

  // this pattern here below is not so sophisticated and will pass some urls like http://...asdf.ads.././.asdf.php but as for now it's good enough for me

  $url_sPattern = '(((http|https)://[\w\-]+[\w\-\.]+[\w/\.]*))i';

  return preg_replace_callback($url_sPattern, "function_parsing_url_to_html_depending_on_type_of_url", $url_sContent);
}

echo url_create($string_containg_links);
Thanks for directing me on a right track m3rajk.

... regulars... kneel to the master :D

Posted: Wed Jul 16, 2003 8:50 am
by m3rajk
i'm far from the master. i got a bunch of help on here.

i just happened to gor through oreilly's learning perl book before going ot php... and the regular expressions stuff in there was so much better tha any of the times i visited it in a formal class setting in college. i always hated them before then cause i was like everyone else and struggled to get them right. now i'm much better. that one got me to get it right most of the time, so i avoided ereg fromt he start here since i like perl's handling