Page 1 of 2
preg_match - How do I...?
Posted: Mon May 06, 2002 4:58 pm
by sweahe
Say I want to find in a string the word 'car' and after that it can be anything any lenght but the word 'hotel' and after that it can be anything any lenght and then the word 'hello' should come.
How would that perl regex look like?? How do I write to not allowing a word like 'hotel' inside this string?
/Andreas
Posted: Mon May 06, 2002 5:23 pm
by fatal
You should read up on preg_match(): preg_match ( string pattern, string subject [, array matches]).
Code: Select all
<?php
$string = "the car is parked in the hotel";
if( preg_match("hotel", $string) )
{
echo"hello";
}
?>
Hope thats what you wanted

Posted: Mon May 06, 2002 5:27 pm
by sweahe
Nope!
Say I want car --- NOT: parked --- hotel
Try this:
Code: Select all
<?php
$string = "the car is parked in the hotel, no the car run into the hotel!";
...???
?>
What I want it to match here is:
car run into the hotel
not:
car is parked in the hotel
Posted: Mon May 06, 2002 5:36 pm
by fatal
Can you be more descriptive in what string(exactly) you want searched, what word your looking for, and how to handle the word when it is found.
Posted: Mon May 06, 2002 5:43 pm
by sweahe
Exactly what I want to find is this:
Code: Select all
$text = 'This is a test <a href="http://www.company.com">test</a>some text<a href="#">muppo</a>whackoo!!';
I want it to match first '<a ' then following anything but '</a>' and then anything until 'muppo'
So it would find:
<a href="#">muppo
And not:
<a href="http://www.company.com">test</a>some text<a href="#">muppo
Is it clear?
Thanks!
Andreas
Posted: Mon May 06, 2002 6:13 pm
by sweahe
How can I do a not this word thing??
I know I can do a not any of these chars like:
[^fghav]
..but how can I prevent a full word like: '</a>' ?
Posted: Mon May 06, 2002 6:14 pm
by fatal
Code: Select all
$text = 'This is a test <a href="http://www.company.com">test</a>some text<a href="#">muppo</a>whackoo!!';
if( preg_match("<a href="#"></a>$", $text)
{
echo"true";
}
???
Posted: Mon May 06, 2002 6:22 pm
by sweahe
Que? I'm sorry I don't think you understand what I want to do here...
Posted: Mon May 06, 2002 6:33 pm
by fatal
To be honest, i dont think you know what you want.
Posted: Mon May 06, 2002 6:36 pm
by sweahe
Sure I do!

Posted: Mon May 06, 2002 7:55 pm
by sweahe
Almost there now:
$search_pattern = '/<[aA] [^<][^/][^aA]*muppo/';
This finds:
<a href="#">muppo
But why can't I put in the last character too the > in </a>
Like this:
$search_pattern = '/<[aA] [^<][^/][^aA][^>]*muppo/';
This does not work!?? hmm....
Any ideas?
Posted: Mon May 06, 2002 11:38 pm
by dusty
Code: Select all
<?
$text = "This is a test <a href="http://www.company.com">test</a> some text <a href="#">muppo</a> whackoo!!";
if(preg_match("/<a href=(.*)</a>/i",$text,$match)) {
echo $matchї0];
}
?>
in your next post, give an example of what you expect the results to be.
Posted: Tue May 07, 2002 7:42 am
by enygma
pftt....use the ereg* functions

</just to confuse things more>
Isn't ereg slower?
Posted: Tue May 07, 2002 8:37 am
by sweahe
I've heard ereg is slower than preg!?
Is it easier done with ereg?
Posted: Tue May 07, 2002 12:47 pm
by dusty
when it comes to regex PERL > all
