Jump to content

Preg_Match For Html Tags


GD77
 Share

Recommended Posts

ok I m using the following and it s working:

 

preg_match('/^[a-z0-9_\/a\-\<li\>(.*?)<\/li\>\ ]+$/i',$va1)

 

need to escape new line character cause m using a wysiwyg editor on a textarea apparently when i m submiting with new line it returns False...

Link to comment
Share on other sites

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?

Edited by Christian F.
Link to comment
Share on other sites

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] ?

Edited by jazzman1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • 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.