在特定单词出现X时分解php字符串

在特定单词出现X时分解php字符串,php,Php,如何根据不断变化的计数选择字符串的内容?每次循环运行时,计数递增1,并且需要字符串的下一部分 $mystring = 'This is my string. This string is a sample. This is a problem'; 所以如果$i==1,那么我想要 echo $newstring // This is my string. 在$i==2时,我想要 echo $newstring // This string is a sample. 在$i==3时,我想要

如何根据不断变化的计数选择字符串的内容?每次循环运行时,计数递增1,并且需要字符串的下一部分

$mystring = 'This is my string. This string is a sample. This is a problem';
所以如果$i==1,那么我想要

echo $newstring // This is my string.
在$i==2时,我想要

echo $newstring // This string is a sample.
在$i==3时,我想要

   echo $newstring // This is a problem.

我已经看过很多关于explode、substr、array_pop等的参考页面,但是我还没有看到一种允许触发字的位置根据递增计数器进行更改的方法。

如果
是要分解字符串的部分,那么可以使用正则表达式

$line = 'This is my string. This string is a sample. This is a problem.';
preg_match("/([[:alpha:]|\s]+\.)/i", $line, $match);

echo $match[1]; 
范例

如果
是要分解字符串的部分,则可以使用正则表达式

$line = 'This is my string. This string is a sample. This is a problem.';
preg_match("/([[:alpha:]|\s]+\.)/i", $line, $match);

echo $match[1]; 
范例
这可以用

您还可以访问每个元素:

$matches = preg_split('/[.?!]/',$mystring);
echo $matches[0]; // This is my string
echo $matches[1]; // This string is a sample
echo $matches[2]; // This is a problem

这可以用一个简单的答案来回答

您还可以访问每个元素:

$matches = preg_split('/[.?!]/',$mystring);
echo $matches[0]; // This is my string
echo $matches[1]; // This string is a sample
echo $matches[2]; // This is a problem

我找到了一个解决方案,虽然这可能不是最干净或最好的方法,但它确实有效。 $shippingData包含

Shipping (<div class="AdvancedShipperShippingMethodCombination"><p class="AdvancedShipperShippingMethod">Free Shipping <br />1 x IARP IA313200 Door Gasket</p> <p class="AdvancedShipperShippingMethod">Economy Delivery (1Kg) <br />1 x WIP69457. Whirlpool Part Number 481946669457</p></div>)';
运输(

免费运输
1个IARP IA313200门垫圈

经济型交付(1Kg)
1个WIP69457.惠而浦零件号481946669457

);
使用的代码:

$shippingData = $order_result->fields['shipping_method'];
$matches = preg_split("/(AdvancedShipperShippingMethod\">)/", $shippingData);
$method = $matches[$n]; //$n is a count that increments with while/next loop
$method = substr($method, 0, strpos($method, "<br />"));
$method = "Shipping (".$method.")";
}
$shippingData=$order\u result->fields['shipping\u method';
$matches=preg_split(“/(AdvancedShippingMethod\”>)/”,$shippingData);
$method=$matches[$n];//$n是随while/next循环递增的计数
$method=substr($method,0,strpos($method,
)); $method=“Shipping”(“.$method.”)为“; }
我找到了一个解决方案,尽管这可能不是最干净或最好的方法,但它确实有效。 $shippingData包含

Shipping (<div class="AdvancedShipperShippingMethodCombination"><p class="AdvancedShipperShippingMethod">Free Shipping <br />1 x IARP IA313200 Door Gasket</p> <p class="AdvancedShipperShippingMethod">Economy Delivery (1Kg) <br />1 x WIP69457. Whirlpool Part Number 481946669457</p></div>)';
运输(

免费运输
1个IARP IA313200门垫圈

经济型交付(1Kg)
1个WIP69457.惠而浦零件号481946669457

);
使用的代码:

$shippingData = $order_result->fields['shipping_method'];
$matches = preg_split("/(AdvancedShipperShippingMethod\">)/", $shippingData);
$method = $matches[$n]; //$n is a count that increments with while/next loop
$method = substr($method, 0, strpos($method, "<br />"));
$method = "Shipping (".$method.")";
}
$shippingData=$order\u result->fields['shipping\u method';
$matches=preg_split(“/(AdvancedShippingMethod\”>)/”,$shippingData);
$method=$matches[$n]//$n是随着while/next循环递增的计数
$method=substr($method,0,strpos($method,
)); $method=“Shipping”(“.$method.”)为“; }

字符串是一个纯样本。如果你想在一个特定的词上爆炸怎么办?是的,你可以做到。实际上,正则表达式在这类事情上非常方便。只是想告诉你我们可以用正则表达式做什么。在这句话中,我们从句子中删除了国家代码。这对我来说就像黑魔法!我对所有的\和/(更不用说[]中的内容)感到困惑。如果我想搜索AdvancedShipperShippingMethod的所有实例,并能够使用$match[1]、$match[2]等访问它们,我将如何格式化正则表达式?该字符串是一个纯示例。如果你想在一个特定的词上爆炸怎么办?是的,你可以做到。实际上,正则表达式在这类事情上非常方便。只是想告诉你我们可以用正则表达式做什么。在这句话中,我们从句子中删除了国家代码。这对我来说就像黑魔法!我对所有的\和/(更不用说[]中的内容)感到困惑。如果我想搜索AdvancedShipperShippingMethod的所有实例,并能够使用$match[1]、$match[2]等访问它们,我将如何格式化正则表达式?在PHP中,迭代通常从
0
开始。将您的输入与其他人的回答分开是正确的方法。另外,如果要拆分的单词都是单个字符,请尝试
strok()
。如果您的问题有一个真实的例子(需要这个-尝试这个),您可能会得到更好的答案。解决方案可能与假设的算法不同。在您下面的评论中,我看到您的字符串来自html,“单词”实际上是其中的
class
属性。有解析程序。@shubder该字符串实际上是shipping方法的db条目。在我正在查看的应用程序中,所有html内容都是多余的。我只想从最初存储的东西中获得“免费送货”、经济货(1Kg)等,但必须能够以给定的顺序获得它们。在PHP中,迭代通常从
0
开始。将您的输入与其他人的回答分开是正确的方法。另外,如果要拆分的单词都是单个字符,请尝试
strok()
。如果您的问题有一个真实的例子(需要这个-尝试这个),您可能会得到更好的答案。解决方案可能与假设的算法不同。在您下面的评论中,我看到您的字符串来自html,“单词”实际上是其中的
class
属性。有解析程序。@shubder该字符串实际上是shipping方法的db条目。在我正在查看的应用程序中,所有html内容都是多余的。我只想从最初存储的任何东西中获得“免费配送”、经济配送(1Kg)等,但必须能够以给定的顺序获得它们。如果$mystring=”Shipping(

免费配送
1 x IARP IA313200门垫圈

经济型交付(1Kg)
1 x WIP69457。惠而浦零件号481946669457

)$chr='AdvancedShipperShippingMethod“>”;我将“/[.?!]”更改为“/[AdvancedShipperShippingMethod?!]/'echo$matches没有提供任何信息。您能告诉我您希望该字符串的输出是什么吗?在示例字符串中,我希望找到$matches[0];//免费配送和$matches[1];//经济配送(1Kg)哦,好的,那么您需要将其解析为html,您认为会起作用吗?如果$mystring='Shipping(

免费送货
1 x IARP IA313200门垫圈

经济型送货(1Kg),您能解释一下为什么您的示例不起作用吗
1 x WIP69457.惠而浦零件号481946669457

);$chr='AdvancedShipperSh