Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > PHP: Тексты > Замена URL адреса картинки


Автор: MoLeX 19.10.2010, 10:54
Добрый день.
Есть некий текст (красным отмечены не постоянные данные, т.е. они могут меняться)
Цитата

<p> This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do <strong class="command">global $variable;</strong> to access it within functions or methods.

<img alt="" src="http://127.0.0.1/admin/ckfinder/core/connector/php/connector.php?command=Thumbnail&amp;type=Images&amp;currentFolder=%2Fbanner%2F&amp;langCode=ru&amp;hash=a68655d5d08e9b12&amp;FileName=1287468124.jpg" style="width: 46px; height: 100px;" /></p>


Необходимо преобразовать URL данной картинки в нечто такое:
Цитата

http://127.0.0.1/img/banner/1287468124.jpg
 

То есть, из строки нужно взять две части: currentFolder и FileName

Автор: patap 19.10.2010, 11:19
Код

  $text = 'text here';

  $regexp = '#(http://.*?)/.*?currentfolder=(.*?)&.*?filename=(.*?\.[a-z]{1,4})#i';

  preg_match($regexp, $text, $matches);
  $url = urldecode($matches[1] . $matches[2] . $matches[3]);
  echo $url;



эм.. или нужно в тексте сделать замену ссылки?

так можно:
Код

  $regexp = '#(http://.*?)/admin/.*?currentfolder=(.*?)&.*?filename=(.*?\.[a-z]{1,4})#i';

  function url($matches) {
    return urldecode($matches[1] . 'img' . $matches[2] . $matches[3]);
  }
  
  $text = preg_replace_callback($regexp, 'url', $text);

Автор: enoff 19.10.2010, 14:02
Код

$regexp = '#(http://.*?)/admin/.*?currentfolder=(.*?)&.*?filename=(.*?\.[a-z]{1,4})#ie';
$text = preg_replace($regexp, "urldecode('$1'.'img'.'$2'.'$3')", $text);

а можно без callback функции обойтись smile 

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)