【问题标题】:PHP place <p> around string not around <div> [closed]PHP将<p>放在字符串周围而不是<div>周围[关闭]
【发布时间】:2014-09-06 16:06:30
【问题描述】:

假设我发布了以下内容:

喜欢这条推文 https://twitter.com/yarratrams/status/489336308423491584

那么下面的脚本将运行下面的代码的以下部分:

$string=str_replace($links[$j],'<a href="'.$links[$j].'" rel="nofollow" target="_blank">'.$links[$j].'</a>',$string).$stats->html;

它会像下面这样结束

Love this tweet <a href="https://twitter.com/yarratrams/status/489336308423491584" rel="nofollow" target="_blank">https://twitter.com/yarratrams/status/489336308423491584</a><iframe id="twitter-widget-0" scrolling="no" frameborder="0" allowtransparency="true" class="twitter-tweet twitter-tweet-rendered" title="Embedded Tweet" height="186" style="display: block; max-width: 99%; min-width: 220px; padding: 0px; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; margin: 10px 0px; border-color: rgb(238, 238, 238) rgb(221, 221, 221) rgb(187, 187, 187); border-width: 1px; border-style: solid; box-shadow: rgba(0, 0, 0, 0.14902) 0px 1px 3px; position: static; visibility: visible; width: 500px;"></iframe>
<script async="" src="//platform.twitter.com/widgets.js" charset="utf-8"></script>   

我想将以下部分包含在&lt;p&gt;&lt;/p&gt;中:

喜欢这条推文https://twitter.com/yarratrams/status/489336308423491584

所以最终结果应该是这样的

<p>Love this tweet <a href="https://twitter.com/yarratrams/status/489336308423491584" rel="nofollow" target="_blank">https://twitter.com/yarratrams/status/489336308423491584</a></p><iframe id="twitter-widget-0" scrolling="no" frameborder="0" allowtransparency="true" class="twitter-tweet twitter-tweet-rendered" title="Embedded Tweet" height="186" style="display: block; max-width: 99%; min-width: 220px; padding: 0px; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; margin: 10px 0px; border-color: rgb(238, 238, 238) rgb(221, 221, 221) rgb(187, 187, 187); border-width: 1px; border-style: solid; box-shadow: rgba(0, 0, 0, 0.14902) 0px 1px 3px; position: static; visibility: visible; width: 500px;"></iframe>
<script async="" src="//platform.twitter.com/widgets.js" charset="utf-8"></script>              </div>

目前,当有人发表评论并包含推文、YouTube 或 Vimeo 链接或将图像上传到我们的亚马逊服务器时,我会执行以下操作:

function autolink($string,$uid = null){
    // force http: on www.
    $string = str_ireplace( "twitter.com", "http://twitter.com", $string );
     $string = str_ireplace( "http://twitter.comhttp://twitter.com", "http://twitter.com", $string );
    $string = str_ireplace( "www.", "http://", $string );
    $string = str_ireplace( "http://http://", "http://", $string );
    // eliminate duplicates after force
    $string = str_ireplace( "http://http://www.", "http://", $string );
    $string = str_ireplace( "https://http://www.", "https://", $string );

    // The Regular Expression filter
    $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
    // Check if there is a url in the text

    $m = preg_match_all($reg_exUrl, $string, $match); 

    if ($m) { 
        $links=$match[0]; 
        for ($j=0;$j<$m;$j++) { 

            if(substr($links[$j], 0, 14) == 'http://youtube'){

            $string=str_replace($links[$j],'<a href="'.$links[$j].'" rel="nofollow" target="_blank">'.$links[$j].'</a>',$string).'<div class="embed-container"><iframe src="//www.youtube.com/embed/'.substr($links[$j], -11).'" frameborder="0"></iframe></div>';

            }
            elseif(substr($links[$j], 0, 12) == 'http://vimeo'){

            $status = file_get_contents("http://vimeo.com/api/oembed.json?url=".$links[$j]);
            $vim = json_decode($status);
            $embed = preg_replace(array('/width="\d+"/i', '/height="\d+"/i'),array('', ''),$vim->html);
            //$string = "https://api.twitter.com/1/statuses/oembed.json?url=".$links[$j];
            $string=str_replace($links[$j],'<a href="'.$links[$j].'" rel="nofollow" target="_blank">'.$links[$j].'</a>',$string)."<div class='embed-container'>".$embed."</div>";

            }
            elseif(substr($links[$j], 0,16) == 'http://au.images')
            {

            $string=str_replace($links[$j],null,$string).'<div class="imgover"><a href="'.$links[$j].'" data-rel="shadowbox['.rand(0,57475676).']"><img alt="'.$links[$j].'" src="'.$links[$j].'"></a></div>';

            }
            elseif(substr($links[$j], 0,16) == 'http://instagram')
            {
                http://api.instagram.com/oembed?url=http://instagram.com/p/V8UMy0LjpX/
                $status = file_get_contents("http://api.instagram.com/oembed?url=".$links[$j]);
                $stats = json_decode($status);
            //$string = "https://api.twitter.com/1/statuses/oembed.json?url=".$links[$j];
            if(isset($stats->errors))
            {

            }
            else
            $string=str_replace($links[$j],'<a href="'.$links[$j].'" target="_blank">'.$links[$j].'</a>',$string).'</p><div class="imgover"><a href="'.$stats->url.'" data-rel="shadowbox['.rand(0,57475676).']"><img  alt="'.$links[$j].'" src="'.$stats->url.'" ></a></div>';


            }
            elseif(substr($links[$j], 0, 15) == 'https://twitter'){

            $status = file_get_contents("https://api.twitter.com/1/statuses/oembed.json?url=".$links[$j]);
            $stats = json_decode($status);
            //$string = "https://api.twitter.com/1/statuses/oembed.json?url=".$links[$j];
            if(isset($stats->errors))
            {

            }
            else
            $string=str_replace($links[$j],'<a href="'.$links[$j].'" rel="nofollow" target="_blank">'.$links[$j].'</a>',$string).$stats->html;

            }
            else{
            $string=str_replace($links[$j],'<a href="'.$links[$j].'" rel="nofollow" target="_blank">'.$links[$j].'</a>',$string);
            } 
        } 
    } 

但是我的问题是我想在未更改的字符串周围放置一个&lt;p&gt;&lt;/p&gt; - 因为当我在外部 div 上放置填充时,它似乎移动了所有内部 DIV,这是修复它的一种方法是将&lt;p&gt;&lt;/p&gt; 放在修改后的字符串之前的文本中。

【问题讨论】:

    标签: php html css str-replace


    【解决方案1】:

    您好,您可以像 echo '&lt;p&gt;'.$string.'&lt;/p&gt;'; 那样连接

    【讨论】:

    • 不,我不能,这不是有效的 html5 标准,你可以在 p 中有一个 div
    • 那我真的不明白你的问题
    • 所以你想将段落包装在一个 div 中?
    • 我已经添加了包含 p 后的外观
    • 这有帮助吗$string=str_replace($links[$j],'&lt;p&gt;&lt;a href="'.$links[$j].'" rel="nofollow" target="_blank"&gt;'.$links[$j].'&lt;/a&gt;&lt;/p&gt;',$string).$stats-&gt;html;
    【解决方案2】:

    @thx 致 Daniel Sirakoff 这是玩过他评论的代码后的正确代码

       $string=str_replace($links[$j],'<a href="'.$links[$j].'" rel="nofollow" target="_blank">'.$links[$j].'</a>','<p>'.$string.'</p>').'<div class="twitterembed">'.$stats->html.'</div>';
    

    【讨论】:

      猜你喜欢
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      相关资源
      最近更新 更多