0
$pattern = '/<a\s+href=["\']([^"\']+)["\']/i';
$desc_feed = preg_replace($pattern, '<a href="https://www.facebook.com/something"', $block->get_description());

This is the regex I use to filter for URL which works well. However, I just want to target a certain type of URL, namely I just want to replace the entire URL ONLY if it contains a certain substring: //l.face... (without the dots)

any help appreciated.

Sebastian
  • 38
  • 5
  • is it specific to some language like javascript or php , you can add that in tag. So, users having expertise this that technolody will help you with same – Panther Dec 23 '14 at 16:28
  • please look at the first answer here http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags THE PONY, HE COMES – Logan Murphy Dec 23 '14 at 16:28
  • What are you actually trying to do? You've got HTML in a string? From a trusted/untrusted source? And you want to rewrite some link targets in that HTML? Don't tell us about substring matches -- tell us about requirements that an end user or user agent might see. – Mike Samuel Dec 23 '14 at 16:29
  • **Don't use regular expressions to parse HTML. Use a proper HTML parsing module.** You cannot reliably parse HTML with regular expressions, and you will face sorrow and frustration down the road. As soon as the HTML changes from your expectations, your code will be broken. See http://htmlparsing.com/php or [this SO thread](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) for examples of how to properly parse HTML with PHP modules that have already been written, tested and debugged. – Andy Lester Dec 23 '14 at 16:30
  • You are probably looking for preg_replace_callback – Gerd K Dec 23 '14 at 16:31

1 Answers1

0

Disclaimer: Don't parse html with regex !!

But, if you have too, this might work for your test case.

Find: '~(?s)<a\s+href=(["\'])(?:(?!\1).)*?//l\.face(?:(?!\1).)*?\1~' Replace: whatever you want

 (?s)                          # Dot all
 <a \s+ href= 
 ( ["'] )                      # (1), Delimiter
 (?:
      (?! \1 )                      # Any char except delimiter
      . 
 )*?
 //l \. face                   # What you're looking for
 (?:
      (?! \1 )                      # Any char except delimiter
      . 
 )*?
 \1                            # Backref to delimiter