PHP script with regular expressions
I'm trying to get the text between the heading tag using the following php script:
$search_string= < h1 >testing here< /h1 >;
$text = preg_match('<%TAG%[^>]*>(.*?)</%TAG%>',$开发者_StackOverflow社区search_string, $matches);
echo $matches[0];
When i try to run this script there is no value being returned. Instead there is warning message: Warning: preg_match() [function.preg-match]: Unknown modifier '(' in C:\xampp\htdocs\check_for_files.php on line 10
Can anyone help with this please?
Your expression needs delimiters. /
is the most common, but #
should work for this situation.
$text = preg_match('#<%TAG%[^>]*>(.*?)</%TAG%>#',$search_string, $matches);
The warning is because you've not enclosed your regex in delimiters. So try
$text = preg_match('#<%TAG%[^>]*>(.*?)</%TAG%>#',$search_string, $matches);
Understanding the warning.
Consider your regex:
'<%TAG%[^>]*>(.*?)</%TAG%>'
^ ^
start end
Since you've not explicitly put the regex between delimiter, PHP thinks you are using <
and >
as delimiter as <
is the first char in the regex. Hence when it sees an un-escaped <
it takes it as end of pattern. Next we can have few modifiers after the closing delimiter which allow us to alter the behavior of the pattern matching. Some commmon modifiers are:
i
for case insensitivem
for multi line match
Now in your case there is a (
after the closing delimiter which is not a valid modifier, hence the warning.
/^<[^>]+>(.*)<\/[^>]+>$/
should do the trick.
精彩评论