Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg_match和preg_使用url和图像标记替换文本_Php_Url_Preg Replace_Preg Match - Fatal编程技术网

Php preg_match和preg_使用url和图像标记替换文本

Php preg_match和preg_使用url和图像标记替换文本,php,url,preg-replace,preg-match,Php,Url,Preg Replace,Preg Match,我正在编写一些代码,我已经做了足够多的工作来启动一些程序。我想替换该文本体中的图像url和web链接 例如“这是我的文字和一些图片” 替换为“这是我的文本,带有和一些图像” 我的黑客让我替换url或img链接,但不是两者都替换。一个是因为顺序而写得太多了 $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; $reg_exImg = '/(http|https|ftp|ftps)\

我正在编写一些代码,我已经做了足够多的工作来启动一些程序。我想替换该文本体中的图像url和web链接

例如“这是我的文字和一些图片”

替换为“这是我的文本,带有
和一些图像

我的黑客让我替换url或img链接,但不是两者都替换。一个是因为顺序而写得太多了

$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|u exUrl=“/(http | https | ftp | ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/”;
$reg|u exImg='/(http | https | ftp | ftps)\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?(jpg | png | gif | jpeg)/';
$post=“这是我的文字,带有http://www.google.com 还有一些图像http://www.somewebimage.png";
if(预匹配($reg\U exImg,$post,$img)){
$img\U post=预更换($reg\U exImg,“,$post);
}否则{
$img_post=$post;
}
if(preg_match($reg_exUrl,$post,$url)){
$img_post=预更换($reg_exUrl,“,$post);
}否则{
$img_post=$post;
}

如果我阻止$reg_exUrl代码块,如果它运行I get the url链接,我将获得图像链接。

您可以在一次传递中完成此操作,您的两种模式非常相似,很容易构建处理这两种情况的模式。使用
preg\u replace\u 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);
$post=“这是我的文字http://www.google.com 还有一些图像http://www.domain.com/somewebimage.png";
#该模式非常基本,可以改进以处理更复杂的URL
$pattern='~\b(?:ht | f)tps?://[a-z0-9.-]+\[a-z]{2,3}(?://\S*)?~i';
$imgExt=['.png'、'.gif'、'.jpg'、'.jpeg'];
$callback=函数($m)使用($imgExt){
if(false==$extension=parse\u url($m[0],PHP\u url\u PATH))
返回$m[0];
$extension=strtolower(strrchr($extension,'.');
if(在数组中($extension,$imgExt))
返回“”;
#最好通过css规则来实现这一点--^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
返回“”;
};
$result=preg\u replace\u回调($pattern,$callback,$post);

我试图做的是一个简单的提要,其中URL是链接,img URL是嵌入的。首先,在将模式与
preg\u replace
一起使用之前,先用
preg\u match
测试一个模式是没有用的。您应该对这两种情况使用一个模式,然后使用
preg\u replace\u callback
选择替换掩码。这样,所有操作都在一个过程中完成,没有任何内容被覆盖。在回调函数中,您可以使用
parse_url
explode
轻松提取文件扩展名。效果很好,是的,我知道只有一种简单的方法无法实现。清晨编码。。