Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.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 多次拆分多个字符串_Php - Fatal编程技术网

Php 多次拆分多个字符串

Php 多次拆分多个字符串,php,Php,在我的数据库中的一个字符串中,包含多个图像,所有排序: url/image-1.jpg|text 1# url/image-2.jpg|text 2# url/image-3.jpg|text 3# 嗯,我需要为每个图像添加新的带有alt属性的img标记。以下是代码: <img src="url/image-1.jpg" alt="text 1"> <img src="url/image-2.jpg" alt="text 2"> <img src="url/ima

在我的数据库中的一个字符串中,包含多个图像,所有排序:

url/image-1.jpg|text 1#
url/image-2.jpg|text 2#
url/image-3.jpg|text 3#
嗯,我需要为每个图像添加新的带有alt属性的img标记。以下是代码:

<img src="url/image-1.jpg" alt="text 1">
<img src="url/image-2.jpg" alt="text 2">
<img src="url/image-3.jpg" alt="text 3">

我试过使用explode和for循环,但它不起作用

$images = "
url/image-1.jpg|text 1#
url/image-2.jpg|text 2#
url/image-3.jpg|text 3#";

$parts = explode("#", $images);
for($i = 0; $i < count($parts); $i++){
    $parts2[$i] = explode("|", $parts[$i]);
    for($y=0; $y < count($parts2[$i]); $y++){        

        echo '<img src="'.trim($parts2[$y]).'" alt="$part[0]" />';            
    }
}
$images=”
url/image-1.jpg | text 1#
url/image-2.jpg | text 2#
url/image-3.jpg | text 3#“;
$parts=分解(“#”,$images);
对于($i=0;$i
您可以使用explode两次

$str ="url/image-1.jpg|text 1#
url/image-2.jpg|text 2#
url/image-3.jpg|text 3#";

$str_row = explode('#',$str);

for ($i=0; $i<count($str_row)-2; $i++){
  $parts = explode('|',$str_row[$i] );
  echo '<img src="'.$parts[0] . " alt=" . $parts[1] .">";

}
$str=“url/image-1.jpg | text 1#
url/image-2.jpg | text 2#
url/image-3.jpg | text 3#“;
$str_row=explode('#',$str);

对于($i=0;$i您可以使用explode两次

$str ="url/image-1.jpg|text 1#
url/image-2.jpg|text 2#
url/image-3.jpg|text 3#";

$str_row = explode('#',$str);

for ($i=0; $i<count($str_row)-2; $i++){
  $parts = explode('|',$str_row[$i] );
  echo '<img src="'.$parts[0] . " alt=" . $parts[1] .">";

}
$str=“url/image-1.jpg | text 1#
url/image-2.jpg | text 2#
url/image-3.jpg | text 3#“;
$str_row=explode('#',$str);

对于($i=0;$i我想分享一些代码

 $string="url/image-1.jpg|text 1#
   url/image-2.jpg|text 2#
   url/image-3.jpg|text 3#";

$imgs=explode("#", $string);

foreach ($imgs as &$img) {
    $image=explode("|", $img);
    echo "<img src='".$image[0]."' alt='".$image[1]."'>";
}
$string=“url/image-1.jpg | text 1#
url/image-2.jpg | text 2#
url/image-3.jpg | text 3#“;
$imgs=explode(“#”,$string);
foreach($imgs as和$img){
$image=explode(“|”,$img);
回声“;
}

我想和大家分享一些代码

 $string="url/image-1.jpg|text 1#
   url/image-2.jpg|text 2#
   url/image-3.jpg|text 3#";

$imgs=explode("#", $string);

foreach ($imgs as &$img) {
    $image=explode("|", $img);
    echo "<img src='".$image[0]."' alt='".$image[1]."'>";
}
$string=“url/image-1.jpg | text 1#
url/image-2.jpg | text 2#
url/image-3.jpg | text 3#“;
$imgs=explode(“#”,$string);
foreach($imgs as和$img){
$image=explode(“|”,$img);
回声“;
}

您可以使用如下正则表达式来解决此问题:

<?php


// array of urls
$urls = [];


// add 3 urls to the array
array_push($urls, "url/image-1.jpg|text 1#");
array_push($urls, "url/image-2.jpg|text 2#");
array_push($urls, "url/image-3.jpg|text 3#");

// array that contains the extracted values from urls
$matches = [];


// loop through all urls
foreach($urls as $url){
    $match = null;
    // try to match the pattern
    preg_match("/\|(.*)#/", $url, $match);
    // if it does match the pattern then add it to the matches array
    if($match){
        array_push($matches, $match[1]);
    }
}



// print all matches
// outputs:
// text 1
// text 2
// text 3
foreach($matches as $match){
    echo $match . "<br>";
}

?>

您可以使用如下正则表达式来解决此问题:

<?php


// array of urls
$urls = [];


// add 3 urls to the array
array_push($urls, "url/image-1.jpg|text 1#");
array_push($urls, "url/image-2.jpg|text 2#");
array_push($urls, "url/image-3.jpg|text 3#");

// array that contains the extracted values from urls
$matches = [];


// loop through all urls
foreach($urls as $url){
    $match = null;
    // try to match the pattern
    preg_match("/\|(.*)#/", $url, $match);
    // if it does match the pattern then add it to the matches array
    if($match){
        array_push($matches, $match[1]);
    }
}



// print all matches
// outputs:
// text 1
// text 2
// text 3
foreach($matches as $match){
    echo $match . "<br>";
}

?>

嗯,我已经更改了脚本并解决了它。现在它工作正常

$parts = explode("#", $images);        
for($i = 0; $i < count($parts); $i++){
    $teile2 = explode("|", $teile[$i]);                
    for($y = 0; $y < count($teile2[0]); $y++){                    
        echo '<img src="'.$urlAdresse.trim($urlImg).'" alt="'.trim($teile2[1]).'" />'."\n";

    }
} 
$parts=explode(#“,$images);
对于($i=0;$i
嗯,我已经更改了脚本并解决了它。现在它工作正常

$parts = explode("#", $images);        
for($i = 0; $i < count($parts); $i++){
    $teile2 = explode("|", $teile[$i]);                
    for($y = 0; $y < count($teile2[0]); $y++){                    
        echo '<img src="'.$urlAdresse.trim($urlImg).'" alt="'.trim($teile2[1]).'" />'."\n";

    }
} 
$parts=explode(#“,$images);
对于($i=0;$i