Forum Moderators: coopster
$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?
//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!
$pattern = "/".[url=http://us2.php.net/manual/en/function.preg-quote.php]preg_quote[/url]($str,'/')."./i";
...sorry about that :)
$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 :)
So to answer your question, no, addslashes will not work.
If you need further clarification, please ask.
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!';
}
$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!';
}