PHP:在字符串中找到一个值,并使用该位置将字符串一分为二
PHP: Find a value within a string and use that position to split the string into two
- 我需要使用(动态)字符串 (
$string
) 找到 <?target?>
- 取
<?target?>
之前的所有内容赋值给$a
- 取
<?target?>
之后的所有内容赋值给$b
- 在我的例子中,我只有一个
<?target?>
的实例(但是最好有选项 =)
因为我很难解释我想做什么,我给你这个伪代码示例:
$string =
"Pellentesque habitant
morbi tristique senectus
<?target?>
et netus et malesuada
fames ac turpis
egestas.";
FinctionToFindAndSplit ( '<?target?>' );
$a =
"Pellentesque habitant
morbi tristique senectus";
$b =
"et netus et malesuada
fames ac turpis
egestas.";
简单 explode() 应该可行:
list($a, $b) = explode('<?target?>', $string);
如果发现多个 <?target?>
,则取决于您想做什么,您可以按原样使用以丢弃其他参数之后的内容,或者使用第三个参数 2
来限制爆炸和什么是返回。
这应该适合你:
只是 explode()
您的分隔符所在的字符串。另请注意,我将输出限制为 2 个元素!因此,如果字符串中有多个定界符,则只会使用第一个,而忽略其他定界符。
<?php
$arr = explode("<?target?>", $string, 2);
$a = $arr[0];
$b = $arr[1];
?>
- 我需要使用(动态)字符串 (
$string
) 找到 - 取
<?target?>
之前的所有内容赋值给$a
- 取
<?target?>
之后的所有内容赋值给$b
- 在我的例子中,我只有一个
<?target?>
的实例(但是最好有选项 =)
<?target?>
因为我很难解释我想做什么,我给你这个伪代码示例:
$string =
"Pellentesque habitant
morbi tristique senectus
<?target?>
et netus et malesuada
fames ac turpis
egestas.";
FinctionToFindAndSplit ( '<?target?>' );
$a =
"Pellentesque habitant
morbi tristique senectus";
$b =
"et netus et malesuada
fames ac turpis
egestas.";
简单 explode() 应该可行:
list($a, $b) = explode('<?target?>', $string);
如果发现多个 <?target?>
,则取决于您想做什么,您可以按原样使用以丢弃其他参数之后的内容,或者使用第三个参数 2
来限制爆炸和什么是返回。
这应该适合你:
只是 explode()
您的分隔符所在的字符串。另请注意,我将输出限制为 2 个元素!因此,如果字符串中有多个定界符,则只会使用第一个,而忽略其他定界符。
<?php
$arr = explode("<?target?>", $string, 2);
$a = $arr[0];
$b = $arr[1];
?>