Php preg_match 和 preg_replace 带有 url 和图像标签的文本
Php preg_match and preg_replace text with url and image tags
我正在编写一些代码,我已经做了足够多的工作来让事情继续下去。我想替换该文本正文中的图像 url(s) 和网络 links。
E.G "This is my text with http://www.google.com and some image http://www.somewebimage.png"
替换为"This is my text with <a href="http://www.google.com">http://www.google.com</a>
and some image <img src="http://www.somewebimage.png">
"
我的 hack 让我替换了 url(s) 或 img links 但不是两者都..一个因为顺序被覆盖了
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$reg_exImg = '/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?(jpg|png|gif|jpeg)/';
$post = "This is my text with http://www.google.com and some image http://www.somewebimage.png";
if(preg_match($reg_exImg, $post, $img)) {
$img_post = preg_replace($reg_exImg, "<img src=".$img[0]." width='300' style='float: right;'> ", $post);
} else {
$img_post = $post;
}
if(preg_match($reg_exUrl, $post, $url)) {
$img_post = preg_replace($reg_exUrl, "<a href=".$url[0]." target='_blank'>{$url[0]}</a> ", $post);
} else {
$img_post = $post;
}
如果我阻止 $reg_exUrl 代码块,我会得到图像 link 如果它运行,我会得到 url link.
您可以一次性完成,您的两个模式非常相似,并且很容易构建处理这两种情况的模式。使用preg_replace_callback
,可以在回调函数中选择替换字符串:
$post = "This is my text with http://www.google.com and some image http://www.domain.com/somewebimage.png";
# the pattern is very basic and can be improved to handle more complicated URLs
$pattern = '~\b(?:ht|f)tps?://[a-z0-9.-]+\.[a-z]{2,3}(?:/\S*)?~i';
$imgExt = ['.png', '.gif', '.jpg', '.jpeg'];
$callback = function ($m) use ($imgExt) {
if ( false === $extension = parse_url($m[0], PHP_URL_PATH) )
return $m[0];
$extension = strtolower(strrchr($extension, '.'));
if ( in_array($extension, $imgExt) )
return '<img src="' . $m[0] . '" width="300" style="float: right;">';
# better to do that via a css rule --^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
return '<a href="' . $m[0] . '" target="_blank">' . $m[0] . '</a>';
};
$result = preg_replace_callback($pattern, $callback, $post);
我正在编写一些代码,我已经做了足够多的工作来让事情继续下去。我想替换该文本正文中的图像 url(s) 和网络 links。
E.G "This is my text with http://www.google.com and some image http://www.somewebimage.png"
替换为"This is my text with <a href="http://www.google.com">http://www.google.com</a>
and some image <img src="http://www.somewebimage.png">
"
我的 hack 让我替换了 url(s) 或 img links 但不是两者都..一个因为顺序被覆盖了
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$reg_exImg = '/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?(jpg|png|gif|jpeg)/';
$post = "This is my text with http://www.google.com and some image http://www.somewebimage.png";
if(preg_match($reg_exImg, $post, $img)) {
$img_post = preg_replace($reg_exImg, "<img src=".$img[0]." width='300' style='float: right;'> ", $post);
} else {
$img_post = $post;
}
if(preg_match($reg_exUrl, $post, $url)) {
$img_post = preg_replace($reg_exUrl, "<a href=".$url[0]." target='_blank'>{$url[0]}</a> ", $post);
} else {
$img_post = $post;
}
如果我阻止 $reg_exUrl 代码块,我会得到图像 link 如果它运行,我会得到 url link.
您可以一次性完成,您的两个模式非常相似,并且很容易构建处理这两种情况的模式。使用preg_replace_callback
,可以在回调函数中选择替换字符串:
$post = "This is my text with http://www.google.com and some image http://www.domain.com/somewebimage.png";
# the pattern is very basic and can be improved to handle more complicated URLs
$pattern = '~\b(?:ht|f)tps?://[a-z0-9.-]+\.[a-z]{2,3}(?:/\S*)?~i';
$imgExt = ['.png', '.gif', '.jpg', '.jpeg'];
$callback = function ($m) use ($imgExt) {
if ( false === $extension = parse_url($m[0], PHP_URL_PATH) )
return $m[0];
$extension = strtolower(strrchr($extension, '.'));
if ( in_array($extension, $imgExt) )
return '<img src="' . $m[0] . '" width="300" style="float: right;">';
# better to do that via a css rule --^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
return '<a href="' . $m[0] . '" target="_blank">' . $m[0] . '</a>';
};
$result = preg_replace_callback($pattern, $callback, $post);