0

Here is what I have up to this point: The function .*? takes everything until the first "this character". For example $html = preg_replace('/alt=".*?"/', '', $html); replaces everything between alt=" and other quotation mark with nothing. My problem is now I have to deal with multiple characters. Here is the portion of text I want to replace :

<a href="http://feeds.feedburner.com/~ff/TheWindowsClub?a=tjWEu-9hLFk:Jv9oVdSsx2A:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/TheWindowsClub?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/TheWindowsClub?a=tjWEu-9hLFk:Jv9oVdSsx2A:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/TheWindowsClub?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/TheWindowsClub?a=tjWEu-9hLFk:Jv9oVdSsx2A:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/TheWindowsClub?i=tjWEu-9hLFk:Jv9oVdSsx2A:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/TheWindowsClub?a=tjWEu-9hLFk:Jv9oVdSsx2A:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/TheWindowsClub?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/TheWindowsClub?a=tjWEu-9hLFk:Jv9oVdSsx2A:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/TheWindowsClub?d=cGdyc7Q-1BI" border="0"></img></a></div><img src="http://feeds.feedburner.com/~r/TheWindowsClub/~4/tjWEu-9hLFk" height="1" width="1" alt=""/>

Unlike last time I can't use quotation marks or other such character. I have to delete the whole line. One thing I thought about was to do something like this:

$html = preg_replace('/<a href=".*?(alt=""/>)/', '', $html);

I thought that using the above code would find the last portion in this segment and replace everything inside but it replaces nothing. Please suggest what should I do?

After running above line of code the output should be nothing. It should remove all this code block.

Sandip Patel
  • 267
  • 4
  • 14
  • 1
    i can't follow what you are saying, you seem to be referring to some previous question and i dont know where is that.. all details should be here that are needed. Show examples of what end result would be etc. – Muhammad Umer Jun 12 '15 at 02:04
  • 1
    If this is part of a larger HTML document you really should use an HTML parser. I can't tell exactly what it is you're trying to replace though? The alt tag? – Cfreak Jun 12 '15 at 02:05
  • No I am trying to replace all the code. I wrote above. I am editing the code. – Sandip Patel Jun 12 '15 at 02:08
  • Can you provide what you want the output to be? I think the solution here is going to be to use an HTML parser though, not a regex. – chris85 Jun 12 '15 at 02:08
  • I have updated the question @chris85. The output should be nothing. – Sandip Patel Jun 12 '15 at 02:11

1 Answers1

1
<a\s+href.*(alt="[^"]*")?>

or without quotation mark :

<a\s+href.*(alt="[^"]*"){0,1}>

We match everything that starts by <a, is followed by at least one space, then by any character until the character >, before which you may have zero or one iteration of the string alt="" containing anything but a ".

Ivan Gabriele
  • 6,433
  • 5
  • 39
  • 60
  • Ivan, could you please explain the code so that I can modify it in future if need arises. Thanks. – Sandip Patel Jun 12 '15 at 02:13
  • I'm not lazy but I strongly advise you to use a website like https://regex101.com/ that will explain you everything line by line ;) But I added a little explanation and fixed a mistake ! – Ivan Gabriele Jun 12 '15 at 02:14