php - 如何用其他单词替换以@@ 开头并以@@ 结尾的文本中的单词?

标签 php arrays regex function

如何用其他单词替换以@@开头并以@@结尾的单词? 提前致谢

$str = 'This is test @@test123@@';

如何获取test123的位置并替换为另一个

最佳答案

你最好使用正则表达式。

echo $str = preg_replace("~@@(.*?)@@~","This is the replaced text", $str);

Demonstration

编辑答案 .. 由于 OP 在不清楚的上下文中提出问题

因为您要抓取内容。使用 preg_match() 和相同的正则表达式。

<?php
$str = 'This is test @@test123@@';
preg_match("~@@(.*?)@@~", $str, $match);
echo $match[1]; //"prints" test123

关于php - 如何用其他单词替换以@@ 开头并以@@ 结尾的文本中的单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23600928/

相关文章:

php - 以编程方式删除 core_url_rewrite 条目 magento

php - PDO 保持 mysql 连接多长时间?

php - 嵌套 while for table select 仅给出一个结果

php - foreach 循环重复数组中的最后一项

javascript - 在 HTML 中动态显示 JavaScript 数组

arrays - 如何查找至少出现 K 次的数组项

arrays - 如何检查数组中有多少真实项和多少虚假项(SWIFT3)

c# - 查找 xml 中所有不是标签的 'more or less than' 字符

python - 正则表达式,先查找 - Python

php - 正则表达式从字符串中删除主题标签