After processing the regulars in a href there are 2 slashes. I can’t write it so that the url correctly formed. If you notice there are 2 slashes, if with one then everything is fine.

 $outText = '<p><a href="//trading-conditions/precious-metals/xpd">Palladium</a></p>'; $base_url = 'http://site.com'; $outText = preg_replace("/href\s*=\s*(('(\/[^']*)')|(\"(\/[^\"]*)\"))/ism", 'href="'.$base_url.'$3$5"', $outText); 

Help please write correctly.

thank

    1 answer 1

    One of the options:

     <?php $outText = '<p><a href="//trading-conditions/precious-metals/xpd">Palladium</a></p>'; $base_url = 'http://site.com'; $pattern = '%href\s*=\s*(?P<quote>[\'"])/*(/[^\'"]*)(?P=quote)%i'; $outText = preg_replace($pattern, 'href="'.$base_url.'$2"', $outText); var_dump($outText); 

    Result:

     string(85) "<p><a href="http://site.com/trading-conditions/precious-metals/xpd">Palladium</a></p>" 

    The links do not allow only the characters " and ' , but allowed an infinite number of slashes at the beginning (only one remains).

    And your version can be altered as follows:

     $outText = preg_replace("/href\s*=\s*(('\/*(\/[^']*)')|(\"\/*(\/[^\"]*)\"))/is", 'href="'.$base_url.'$3$5"', $outText); 
    • It does not work for me. that is, I replaced html with my own and nothing else - user216109
    • @Sergey, which one? By my example, I checked the work of regulars. - Visman
    • I apologize, my cant, everything works right. Thank you - user216109