Question

I'm looking for tags that end in a specific way using regular expressions in PHP. However all my attempts either result in too much or too little.

For example, in the following string I'd like to match 'bar' because it is in a tag that ends with 'suffix'.

preg_match_all("/<(.*?)suffix>/", "<foo> <barsuffix> <baz>"

However the above line results in 'foo>

Was it helpful?

Solution

Use this regex instead: /<([^>]*)suffix>/.

It will not allow things like foo> <bar, because [^>] ensures that the match souldn't containt >.​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top