字符串变量函数Php

时间:2017-06-26 22:35:11

标签: php string variables tags add

我的英语不好但我想你会明白的。 我正在使用cURL方法获取数据。

我有一个字符串变量。这是。

$contents = 300 g of cooled marzipan raw material approx 35 g of powdered sugar for molding 200 g of dark chocolate couverture 50 skinned half almond kernels

我想要一个函数或方法在每个数字之前添加一个标记(300,35,200,50) 我想以这种方式编辑它。

$contents = <li>300 g of cooled marzipan raw material approx</li><li>35 g of powdered sugar for molding</li><li>200 g of dark chocolate couverture </li><li>50 skinned half almond kernels

找到变量的编号,并在编号前添加list标签。 警告:我尝试了很多功能但结果就是这样。我不想要这个。这些数字将占据整体(300,50,....)

 $contents = </li><li>3</li><li>0</li><li>0 g of cooled marzipan raw material approx</li><li>3</li><li>5 g of powdered sugar for molding</li><li>2</li><li>0</li><li>0 g of dark chocolate couverture </li><li>5</li><li>0 skinned half almond kernels

这个内容每次都在改变。我尝试了太多功能。我认为答案是preg_replace.But我从不使用这种方法。

第二个问题;

我还有一个字符串变量

$contentImg = "<img src="//i.hurimg.com/i/hurriyet/75/194x110/57bbf91967b0a930747c6900.jpg" data-src="//i.hurimg.com/i/hurriyet/75/194x110/594fa5cd18c7732b9c99c4ca.jpg" alt="Türk Malı yeni bölüm ne zaman Star Tv yayın akışında olacak" class="lazy " />"

我想要改变这个变量。我想从$ contentImg变量中做到这一点。

 $contentImg = "<img src="//i.hurimg.com/i/hurriyet/75/194x110/594fa5cd18c7732b9c99c4ca.jpg" alt="Star Tv yayın akısında olacak" class="lazy " />"

结果,我想删除

  

src =“// i.hurimg.com/i/hurriyet/75/194x110/57bbf91967b0a930747c6900.jpg”数据 -

此行来自$ contentImg变量或不同变量。 网址差异不应影响情况。 即使它不同,也必须删除该行。 我希望我能表达自己=) 感谢

1 个答案:

答案 0 :(得分:0)

这是我的方法,我使用explode和is_numeric,然后使用substr函数

    <?php
        $contents = "300 g of cooled marzipan raw material approx 35 g of powdered sugar for molding 200 g of dark chocolate couverture 50 skinned half almond kernels";
        $newwords ="";
        $word= explode(" " , $contents);
        foreach ($word as $value) {
            if (is_numeric($value)){
                $newwords .="</li><li>"." ".$value;
            }
            else { 
                $newwords .=" ".$value;
            }
        }
$newwords=$newwords."<li>";
        $newwords=substr($newwords, 5);
        echo $newwords;
        ?>

对于第二个问题,我不知道这是否是你想要的

<?php
$contentImg = "<img src=\"//i.hurimg.com/i/hurriyet/75/194x110/57bbf91967b0a930747c6900.jpg\" data-src=\"//i.hurimg.com/i/hurriyet/75/194x110/594fa5cd18c7732b9c99c4ca.jpg\" alt=\"Türk Malı yeni bölüm ne zaman Star Tv yayın akışında olacak\" class=\"lazy \" />";
$myvariable=explode("src=\"",$contentImg);
 $newsrc= "i.hurimg.com/i/hurriyet/75/194x110/594fa5cd18c7732b9c99c4ca.jpg";
echo $myvariable["0"]."src=\"".$newsrc."\" alt=\"Star Tv yayın akısında olacak\" class=\"lazy \" />";
?>