regex — как обновить всю привязку href из строки, используя переполнение стека

Я должен обновить все теги href в строке, используя php.
например:

<a href="test.com">test</a> lorum <a href="ola.com">ipsum</a>
so i want to change href as <a href="http://ourdomain.com?url=test.com">test</a>
lorum <a href="http://ourdomain.com?url=ola.com">ipsum</a>

подскажите пожалуйста как обновить все анкеры href

Здесь я делаю так, но не обновляюсь правильно

$replaceStr  = "http://affilate.domain.com?cam=1&url=";
$html="<a href="test.com">test</a> lorum <a href="ola.com">ipsum</a>";
$match = array();
$url   = preg_match_all('/<a [^>]*href="(.+)"/', $html, $match);
if(count($match))
{
for($j=0; $j<count($match); $j++)
{
$html = str_replace($match[1][$j], $replaceStr.urlencode($match[1][$j]), $html);
}
}

-1

Решение

(?<=href=")([^"]*)

Попробуйте это. Заменить на http://ourdomain.com?url=$1. См. Демо.

https://www.regex101.com/r/bC8aZ4/11

$re = "/(?<=href=\")([^\"]*)/im";
$str = "<a href=\"test.com\">test</a> lorum <a href=\"ola.com\">ipsum</a>";
$subst = "http://ourdomain.com?url=$1";

$result = preg_replace($re, $subst, $str);
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector