Php 如何在标记文本之前使用substr?

Php 如何在标记文本之前使用substr?,php,Php,我想在“细节”之前剪切文本 您需要使用substr,它用于获取字符串的子字符串以及strpos,后者给出给定事件的位置: echo substr($text, 0, strpos($text, 'DETAIL')); 访问您可以在数组中拆分文本并获取数组的第一个元素: $text="Name:myproduct Price:99 DETAIL :abcdedsfsdff 21348dfsdf ds "; $abc = explode('DETAIL',$text); echo $abc[0]

我想在“细节”之前剪切文本


您需要使用
substr
,它用于获取字符串的子字符串以及
strpos
,后者给出给定事件的位置:

echo substr($text, 0, strpos($text, 'DETAIL'));

访问

您可以在数组中拆分文本并获取数组的第一个元素:

$text="Name:myproduct Price:99 DETAIL :abcdedsfsdff 21348dfsdf ds ";
$abc =  explode('DETAIL',$text);
echo $abc[0];
这将输出

名称:我的产品价格:99

这将输出

详情:abcdedsfsdff 21348dfsdf ds


工作非常感谢。@ NurmpungUnchalee,你可能要考虑其中一个答案作为公认的答案!
$text="Name:myproduct Price:99 DETAIL :abcdedsfsdff 21348dfsdf ds ";
$abc =  explode('DETAIL',$text);
echo $abc[0];
$text = "Name:myproduct Price:99 DETAIL :abcdedsfsdff 21348dfsdf ds";
$clean = substr($text, 0, stripos($text, "detail"));
echo $clean;
$text = "Name:myproduct Price:99 DETAIL :abcdedsfsdff 21348dfsdf ds";
$clean = substr($text, stripos($text, "detail"), strlen($text) - 1);
echo $clean;