PDA

View Full Version : (.+?) in PHP



Dusty
December 25th, 2000, 16:32
Scenario:

There's a variable containing something like this: "<aaa><bbb>". The object is to find what's between the first two brackets. In Perl, this could be done like this:

$var="<aaa><bbb>";
$var=~/<(.+?)>/;
$result=$1;

My question is, how is this done in PHP? I know:

$var="<aaa><bbb>";
ereg("<(.*)>", $var, $out);
$result=$out[1];

But * is greedy, so instead of "aaa" it gives "aaa><bbb". I tried replacing (.*) with (.+?) but apparently you can't do that in PHP. So, how do you do it?

Dusty
December 27th, 2000, 17:06
Okay, so no one knows. Can anyone point me in the direction of someone who might know?

Technics
December 27th, 2000, 17:22
http://www.sitepointforums.com - someone there should be able to help you.

Dusty
December 31st, 2000, 15:11
I just thought I'd post a follow-up in case anyone else was following the thread:

There is no (.+?) equivalent in PHP and if anyone tells you PHP is as powerful as Perl, you have my permission to give them a good throttling. The only thing PHP is good for, as far as I'm concerned, is database manipulation-- and even then it has its limitations. Easy and fast, maybe, but that's a poor excuse for lack of functionality. I'm just waiting for Perl 6.

Woofcat
December 31st, 2000, 18:53
I just thought I'd reply to this goddamned idiot.

Why the hell are you trying to use Perl crap with the POSIX regular expression functions? If you need to use PERL-COMPATIBLE regex, use the damn PERL-COMPATIBLE REGEX FUNCTIONS, you moron.

I don't usually flame people on message boards, but when some clueless Perl idiot talks s**t about PHP because he's too damn stupid to read the manual I just can't resist.

And here's how to write your stupidass code...

$var="<aaa><bbb>";
preg_match("/<(.+?)>/",$var,$out);
echo $out[1];

PS: Stick with Perl, loser.

Dusty
December 31st, 2000, 20:36
Finally someone answered me! I figured if I angered you PHP people enough you'd reply, but I didn't think it would be that quick. I ought to do that more often.

Ask a question and get no answer, but say it can't be done... well, thanks Woofy.

Dusty
December 31st, 2000, 21:01
I did read the manual, it was the first place I checked. However, the manual really isn't all that helpful and doesn't say that much (but you can see that for yourselves, http://www.php.net/manual/function.ereg.php and http://www.php.net/manual/function.preg-match.php). The most helpful parts of the manual are the user contributed notes. That's not insulting PHP (I only did that to get an answer), I don't find any manual helpful.

The usual way I would had gone about finding out how would had been to download a bunch of free scripts and look for how they did it, but of all the scripts I looked through none of them did anything similar to what I wanted. So, I posted my question (in an easy to follow scenario situation at that) on here, on SitePoint, and on a few other forums I'd never been to before. No one answered me (that's not true, I did get one response at SitePoint but it didn't actually answer my question). So I tried coaxing an response rather than asking for one. Luckily, that method worked. Otherwise I would of had to try to figure it out for myself.

Happy new year, everyone.

jvv
January 1st, 2001, 06:29
You should use the search function on php.net, it always pops up a relevant answer ;)