KenDev KenDev - 4 months ago 9
PHP Question

preg_match remove unwanted text from string

I want to remove unwanted text from string.

$description = 'this is some huge text <a href="http://example.com/dynamicID">dynamicID</a><br> and some more text here';

preg_match('/(example.com([^&]*)?)"/',$description,$matches);

$part = $matches[1];

echo $part; //returning example.com/dynamicID">dynamicID</a><br> and...


But i want only ( example.com/dynamicID ) and remove the rest of the text from string.

What am I missing here?

Answer

You are close. I assume you want to take the URL portion of the text (a.k.a before the closing quote on tag.

All you have to do is to add ?" after ) to ensure that you try to match as shortly as possible any characters before quote.

Doing that, however, the resulting $matches[0] will also include the quote character. You can remedy this by adding group to what you actually want to match and take $matches[1] as your result. Below is your code modified with the 2 changes I mentioned above:

$description = 'this is some huge text <a href="http://example.com/dynamicID">dynamicID</a><br> and some more text here';

preg_match('/(example.com([^&]*)?)"/',$description,$matches);

var_dump($matches);

The output is like below:

array(3) {
  [0]=>
  string(22) "example.com/dynamicID""
  [1]=>
  string(21) "example.com/dynamicID"
  [2]=>
  string(10) "/dynamicID"
}