且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP - 如何在某个字符处拼接字符串?

更新时间:2023-02-26 11:29:39

对于您的情况,请使用:$string="date: March 27, 2017";$string="开始:12:30pm";

For your case, using: $string="date: march 27, 2017"; or $string="start: 12:30pm";

您可以选择以下任何一种技术:

You can select any one of these techniques:

*注意:如果担心针的存在(coloncolon space),那么您应该采用防伪选项之一,否则需要额外的考虑才能在没有针的情况下接住绳子.

*note: If there are concerns about the existence of the needle (colon or colon space) then you should employ one of the options that is false-proof, otherwise additional considerations will be necessary to catch strings without the needle.

使用 strpos() &substr() *防伪:

Use strpos() & substr() *false-proof:

$string=($pos=strpos($string,": "))?substr($string,$pos+2):$string;

使用 str() &substr() *防伪:

Use strstr() & substr() *false-proof:

$string=($sub=strstr($string,": "))?substr($sub,2):$string;

使用explode() *需要冒号空间em> 存在:

Use explode() *requires colon space to exist:

$string=explode(': ',$string,2)[1];

使用 explode() &end() *不再是单线,而是防伪:

Use explode() & end() *no longer a one-liner, but false-proof:

$array=explode(': ',$string,2);
$string=end($array);
// nesting explode() inside end() will yield the following notice:
// NOTICE Only variables should be passed by reference

使用 preg_replace()正则表达式模式 *防伪:

Use preg_replace() with a regex pattern *false-proof:

$string=preg_replace("/(^.*?:\s)/","",$string);

使用 preg_match()正则表达式模式 不是单行的,而是防伪的:

Use preg_match() with a regex pattern not a one-liner, but false-proof:

preg_match("/^.*?:\s(.*)/",$string,$captured);
$string=(sizeof($captured))?end($captured):$string;

使用 str_replace() *不是单行的,但防伪:

Use str_replace() *not a one-liner, but false-proof:

$search=array("date: ","start: ");
$replace="";
$count=1;
$string=str_replace($search,$replace,$string,$count);

这是一个演示,适用于可能想在自己的雪花上运行一些测试的人案例.

Here is a Demo for anyone who might want to run some tests on their own snowflake case.