Forum Moderators: coopster

Message Too Old, No Replies

Preg match question

         

asantos

2:22 am on Jan 1, 2007 (gmt 0)

10+ Year Member



Hi
i have this string:

$str = 'This is a basic string and nobody will ever edit this... everything but this numbers 384, ok?'

Now... i need to know if all that string exists somewhere in $html. Of course... the 384 value wont or doesnt have to be the same.

So, actually, i have to search:

'This is a basic string and nobody will ever edit this... everything but this numbers ###, ok?'

inside $html

### can be 0, 1, 342, 5483, etc.

How can i achieve that through preg_match?

eelixduppy

3:56 am on Jan 1, 2007 (gmt 0)




//validate $num to make sure that it only contains integers before the following
$str = 'This is a basic string and nobody will ever edit this... everything but this numbers '.$num.', ok?';
$pattern = "/".$str"./i";
if(preg_match($pattern,$html_string)) {
echo 'ITS ALIVE!';
} else {
echo 'Not found...';
}

Hope this helps!

eelixduppy

4:12 pm on Jan 1, 2007 (gmt 0)



Actually, $pattern should be this:

$pattern = "/".[url=http://us2.php.net/manual/en/function.preg-quote.php]preg_quote[/url]($str,'/')."./i";

...sorry about that :)

asantos

5:33 pm on Jan 1, 2007 (gmt 0)

10+ Year Member



eelixduppy, thanks a lot for your help. the problem is, i dont know (and i CANT know) what the $num value is.

My best guess is that $num should be a wildcard, which is where i am stuck at.

eelixduppy

5:47 pm on Jan 1, 2007 (gmt 0)



I see now. I was under the impression that you were going to have a number value. The pattern would then look something like this:

$pattern = "/This is a basic string and nobody will ever edit this\.\.\. everything but this numbers [0-9]+ ok\?/i";

In the static string part, make sure that you escape everything that needs to be escaped, otherwise you will get an error.

I hope this answers your question :)

asantos

6:35 pm on Jan 1, 2007 (gmt 0)

10+ Year Member



thanks!
about escaping the static string part, im gonna use addslashes() ... that is enough right?

eelixduppy

6:40 pm on Jan 1, 2007 (gmt 0)



Refer to Pattern Syntax [us2.php.net]. You have to escape all meta characters that you want to be taken as string literals. In my example above, I escaped the period because it is a meta character.

So to answer your question, no, addslashes will not work.

If you need further clarification, please ask.

asantos

7:30 pm on Jan 1, 2007 (gmt 0)

10+ Year Member



hi eelixduppy,
im getting an err message:
"Warning: preg_match(): Unknown modifier 'c'"

This is my code, based in the one you provided to me:

$pattern = '/'.preg_quote('<script type="text/javascript" src="http://123.com/').'[0-9]+'.preg_quote('.js"></script>').'\?/i';

$template = '<html><head><script type="text/javascript" src="http://123.com/41.js"></script></head><body>test</body></html>';

if(preg_match($pattern,preg_quote($template))) {
echo 'matches!';
} else {
echo 'no match!';
}

IanKelley

9:50 am on Jan 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need to escape $pattern, not $template. The second forward slash in the string is being interpreted as end of expression.

eelixduppy

2:22 pm on Jan 2, 2007 (gmt 0)



Here's the whole thing:

$pattern = '/'.preg_quote('<script type="text/javascript" src="http://123.com/',[b]'/'[/b]).'[0-9]+'.preg_quote('.js"></script>',[b]'/'[/b]).'/i';

$template = '<html><head><script type="text/javascript" src="http://123.com/41.js"></script></head><body>test</body></html>';

if(preg_match($pattern,$template)) {
echo 'matches!';
} else {
echo 'no match!';
}

asantos

3:20 pm on Jan 2, 2007 (gmt 0)

10+ Year Member



works!
thank you all