如何在PHP中替换字符串中的单个单词? - php

我需要用数组给定的替换词来代替单词

$words = array(
'one' => 1,
'two' => 2,
'three' => 3
);

$str = 'One: This is one and two and someone three.';

$result = str_ireplace(array_keys($words), array_values($words), $str);

但是此方法将someone更改为some1。我需要替换单个单词。

参考方案

您可以在正则表达式中使用word boundries要求单词匹配。

就像是:

\bone\b

会做到的。您想在PHP中使用带有preg_replace修饰符的i

正则表达式演示:https://regex101.com/r/GUxTWB/1

PHP用法:

$words = array(
'/\bone\b/i' => 1,
'/\btwo\b/i' => 2,
'/\bthree\b/i' => 3
);
$str = 'One: This is one and two and someone three.';
echo preg_replace(array_keys($words), array_values($words), $str);

PHP演示:https://eval.in/667239

输出:

1:这是1和2以及某人3。

PHP strtotime困境 - php

有人可以解释为什么这在我的服务器上输出为true吗?date_default_timezone_set('Europe/Bucharest'); var_dump( strtotime('29.03.2015 03:00', time()) === strtotime('29.03.2015 04:00�…

PHP-全局变量的性能和内存问题 - php

假设情况:我在php中运行一个复杂的站点,并且我使用了很多全局变量。我可以将变量存储在现有的全局范围内,例如$_REQUEST['userInfo'],$_REQUEST['foo']和$_REQUEST['bar']等,然后将许多不同的内容放入请求范围内(这将是适当的用法,因为这些数据指的是要求自…

php-casperjs获取内部文本 - php

我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…

PHP str_replace是否具有大于13个字符的限制? - php

直到击中第13个字符为止。一旦str_ireplace在cyper数组中击中“ a”,str_ireplace就停止工作。数组的大小有限制吗?请记住,如果键入“ abgf”,我会得到“ nots”,但是如果键入“ abgrf”,我应该得到“ notes”,那么我就会得到“ notrs”。机架使我的大脑无法解决。$_cypher = array("n…

PHP-将日期插入日期时间字段 - php

我已在数据库中使用datetime字段存储日期,使用PHP将“今天的日期”插入该字段的正确方法是什么?干杯, 参考方案 我认为您可以使用php date()函数