如何将一个字符串分割成更小的字符串


How to split a string into a smaller part of string

我有一个这样的变量:

$str = 'loremipsum Gol A WB=10 PRC=7|Gol O TC=8 PRC=12|Gol B WB=170 PRC=17|Gol AB WB=0 TC=1 url';

我想将Gol A WB=10, Gol A PRC=7, Gol O TC=8等拆分为一个独立的字符串,这样我可以对字符串处理另一个函数。如有任何帮助,不胜感激。

之前,谢谢你的答案,但我很抱歉你的答案不是我需要的答案。目前为止,我得到了这个

$string_array = array("abc string 1 abc","abc string 2 abc" ,"abc string 3 abc");
foreach($string_array as $string){
echo getBetween($string,"abc","abc") . "<br>";
}

我的问题是我没有相同的单词要删除,而是一个不同的单词,所以我需要的代码,我相信不会是

echo getBetween($string,**"abc","abc"**) . "<br>";

请帮帮我。

$str = 'loremipsum Gol A WB=10 PRC=7|Gol O TC=8 PRC=12|Gol B WB=170 PRC=17|Gol AB WB=0 TC=1 url';
preg_match_all('/Gol(.*?)[0-9]/',$str,$matches);
foreach($matches[0] as $match) {
    echo "<li>{$match}</li>";
}

我不确定你到底是什么意思,但也许其中之一?:

<?php
$str = 'loremipsum Gol A WB=10 PRC=7|Gol O TC=8 PRC=12|Gol B WB=170 PRC=17|Gol AB WB=0 TC=1 url';
preg_match_all('/(Gol [A-Z]{1,2} [A-Z'=0-9]{1,})/',$str,$matched);
print_r($matched[1]);
print_r(preg_replace('/'.implode("|",$matched[1]).'/',"",$str));
print_r(implode("|",$matched[1]));
?>

给你:

Array
(
    [0] => Gol A WB=10
    [1] => Gol O TC=8
    [2] => Gol B WB=170
    [3] => Gol AB WB=0
)
loremipsum  PRC=7| PRC=12| PRC=17| TC=1 url
Gol A WB=10|Gol O TC=8|Gol B WB=170|Gol AB WB=0

编辑:您的原始请求不是很清楚,但您可能需要像包装的preg_match()这样的东西。

function extract_val($str,$reg1="",$reg2="")
    {
        preg_match('/^'.$reg1.'(.*)'.$reg2.'$/', $str,$matched);
        return (isset($matched[1]))? $matched[1]:false;
    }

try:

$str = 'loremipsum Gol A WB=10 PRC=7|Gol O TC=8 PRC=12|Gol B WB=170 PRC=17|Gol AB WB=0 TC=1 url';
$chunks=explode('|', $str); //Split your string by |
foreach($chunks as $chunk){ //iterate through chunks
echo $chunk;
//process here using chunk...
}