[<].* would match all strings that do not contain <
How do we match all strings that do not contain "</"
The simplest thing would be:
if(strpos($string, '</',) === FALSE) { // no "</" in the string }
If you really want to do it with a regex, just negate the function call:
if(!preg_match('#</#', $string)) { // no "</" in the string }
What about if I want to match the last </
So, a </ followed by all characters that doesn't contain </
Hi,
Regex's are good for lots of things, but some seemingly straightforward searches turn out to be really awkward.
Try strrpos.
P
<?php header("Content-Type: text/plain"); $text = "<b>test 1</b> <i>test 2</i> <em>test 3</em> the end."; if(preg_match('#</(.(?!</))*$#', $text, $matches)) { echo $matches[0]; } else { echo "Not found"; }
Output:
</em> the end.
Hey NogDog,
That's really nice.
Can I ask what this part ...
.(?!</)
... is doing? I just can't see it 🙁
Paul
The "(?!" is the start of a look-ahead negative assertion, which is ended by the closing parenthesis. So ".(?!</)" means any character (the dot) which is not followed by "</".
Believe me, I didn't think of that right away -- I had a feeling there was some way to do this with assertions, but it took several wrong turns before I came up with that one.
That's a real beauty!
Gonna add that one to box o' tricks!
Hat's off!
P.
Yeah man, I am bookmarking this.
Good one NogDog.
Now the big question: is that what the OP was really looking for?
GENIUS
Thanks a lot.
Wow....