On the web pages of the site there are links to archives to download the file.

Links of this type:

<a title="" href="http://site.ru/downloads/soft.zip">bestsoft</a> 

It is necessary after each such link to insert an HTML tag, for example <hr /> . Those. after the closing tag </a> tag should be <hr /> .

Here's how:

 <a title="" href="http://site.ru/downloads/soft.zip">bestsoft</a><hr /> 

What did I do:

I found such links on web pages by file extension, regular.

 $html = preg_replace('/<a[^>]+.zip[^"]*">(.*?)<\/a>$/', '<hr />', $html); return $html; 

But the links were replaced by the <hr /> .

Question:

What is the correct code that will add the HTML tag immediately after the links to the archive with the .zip, .rar, .7-zip file extension?

  • 2
    preg_replace('~<a\s[^>]+\.zip[^"]*">.*?</a>$~', '$0<hr />', $html); - Wiktor Stribiżew
  • no changes - Viher
  • You do not have it. And we have. - Wiktor Stribiżew
  • Yes, there is, strange, I will look for an error. SPS - Viher
  • found a mistake, it works like this: $text = preg_replace('/<a[^>]+.zip[^"]*">(.*?)<\/a>/', '$0<hr />', $text); it is possible instead of a slash tilde. Almost broke his head on the regular season. Also, the editor automatically puts the br tag before hr. Thank. - Viher

3 answers 3

Fully working example:

 function addhtml_forlink( $text ) { $html = '<hr />'; $text = preg_replace('/<a[^>]+\.(zip|rar|gz|tar)[^"]*">(.*?)<\/a>/',"$0$html", $text); return $text; } 
     <a title="" href="<?php echo 'you link'; ?>"><?php echo 'you name'; ?></a><hr /> 
    • Try to write more detailed answers. Explain what is the basis of your statement? - Nicolas Chabanovsky

    preg_replace('/(<a.+?\.zip".+?<\/a>)/', '$1<hr />', $subject);