Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.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_replace_回调-我需要它来忽略URL中重复的空格_Php - Fatal编程技术网

Php preg_replace_回调-我需要它来忽略URL中重复的空格

Php preg_replace_回调-我需要它来忽略URL中重复的空格,php,Php,下面是我用来解析URL的当前代码,它适用于MySQL中的所有查询,直到找到URL中的空格为止。然后像这样把事情分开 http://(1).jpg 我需要解决这个问题 <?php $con = mysql_connect("localhost","----","----"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("----", $con); $result =

下面是我用来解析URL的当前代码,它适用于MySQL中的所有查询,直到找到URL中的空格为止。然后像这样把事情分开

http://(1).jpg

我需要解决这个问题

<?php
$con = mysql_connect("localhost","----","----");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("----", $con);

$result = mysql_query("SELECT * FROM posts ORDER BY postid DESC");

while($row = mysql_fetch_array($result))
  {

$str = $row['post_content'];

$str = preg_replace_callback('#(?:https?://\S+)|(?:www.\S+)|(?:\S+\.\S+)#', function($arr)
{
    if(strpos($arr[0], 'http://') !== 0)
    {
        $arr[0] = 'http://' . $arr[0];
    }

    $url = parse_url($arr[0]);

    // images
    if(preg_match('#\.(png|jpg|gif)$#', $url['path']))
    {
        return '<img src="'. $arr[0] . '" />';
    }
    // youtube
    if(in_array($url['host'], array('www.youtube.com', 'youtube.com'))
      && $url['path'] == '/watch'
      && isset($url['query']))
    {
        parse_str($url['query'], $query);
        return sprintf('<iframe class="embedded-video" src="http://www.youtube.com/embed/%s" allowfullscreen></iframe>', $query['v']);
    }
    //links
    return sprintf('<a href="%1$s">%1$s</a>', $arr[0]);
}, $str);

echo $str;
echo "<br />";
}

mysql_close($con);
?>

Url中不应包含空格。看见一个简单的解决方法是在preg_匹配之前对其进行url编码

你应该对东西进行URL编码,但既然你已经存储了,那就试试吧

$str = str_replace(' ', '%20', $str);

$str = preg_replace_callback('#(?:https?://\S+)|(?:www.\S+)|(?:\S+\.\S+)#', function($arr)

Url中不应该有空格。看见一个简单的解决方法是在preg_匹配之前对其进行url编码

你应该对东西进行URL编码,但既然你已经存储了,那就试试吧

$str = str_replace(' ', '%20', $str);

$str = preg_replace_callback('#(?:https?://\S+)|(?:www.\S+)|(?:\S+\.\S+)#', function($arr)

这似乎没有起到任何作用。当用户上传图像时,它会将url发布到数据库。如果它是一个重复的映像,它将成为domain.com/.dirs/image.jpg(1.jpg。。。副本中的空格似乎导致上述代码将其拆分为两个URL。你发布的代码被放置在preg_匹配线的正上方,它会进行任何更改。你能更正粘贴吗。它有三个{和两个}所以很难弄清楚到底发生了什么所有困难的方法都是这样做和挖掘的,很简单吧?谢谢jpiasetz。这似乎没有起到任何作用。当用户上传图像时,它会将url发布到数据库中。如果它是一个重复的映像,它将成为domain.com/.dirs/image.jpg(1.jpg。。。副本中的空格似乎导致上述代码将其拆分为两个URL。你发布的代码被放置在preg_匹配线的正上方,它会进行任何更改。你能更正粘贴吗。它有三个{和两个}所以很难弄清楚到底发生了什么所有困难的方法都是这样做和挖掘的,很简单吧?谢谢你。