Jump to content

Preg_Match For Html Tags


GD77

Recommended Posts

Jazzman: [^\n] matches everything that's not a newline character, exactly the opposite of what GD77 wanted.

 

GD77: Remove the caret from the newline character group, and it should match as you want it to. Here's it a bit cleaned up, and properly escaped for PHP strings too:

'#^[a-z0-9_/a+ -]+<li>(.*?)</li>[\\n\\r]+\\z#i'

Doing this cleanup I found another problem too, namely the fact that you've added the LI tag patter inside the character group. Which caused it to consider the individual characters as part of a group, not as a pattern you wanted matched. I've moved it outside for you, but without any example data I cannot know if this pattern matches what you want.

 

BTW: What's the escaped space after the closing LI-tag?

Ah, my apology. You want to match new lines :happy-04:

$pattern = '/^[\n\r]?[a-z0-9_\/a\-\<li\>(.*?)<\/li\>\ ]+/i';
if(preg_match($pattern, $va1)){
echo 'true';
} else {
echo 'false';
}

 

@Christian, you pattern won't work, why are you using [\\n\\r] ?

Unfortunately, the code posted by Jazzman only seems to be working. It has the same issues that I highlighted for your code, due to the grouping of the li-tags inside the square brackets. For all intents and purposes, this is exactly the same:

'#^[\\n\\r]?[\\w?./<>() *-]+#i'

Note that neither period, asterisk, question marks or parenthesis has any special meaning within a character group.

 

Now, if you want help to making something that actually matches what you want it to match, you need to post an example of the source data. As I requested above.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.