php - 使用 PHP/laravel 8 抓取字符串中的标签
问题描述
我正在使用 PHP/Laravel 8,我正在尝试获取字符串的每个标签(以 # 开头的单词)并编辑字符串以在其中设置锚点。
例如:
$string = "Hello, this is a #string with multiples #tags";
//方法
=>
$newString = "Hello, this is a <a href='/string'>#string</a> with multiples <a href='/tags' >#tags</a>"
我尝试了很多东西,但从那一刻起我就卡住了......
谢谢你的帮助...
解决方案
使用preg_replace()
#[^ ]+
$string = "Hello, this is a #string with multiples #tags";
echo preg_replace('/#([^ ]+)/', '<a href="/$1">$0</a>', $string);
推荐阅读
- c - 错误 C2088:“+=”:在 Visual Studio 上使用 c 的联合是非法的
- reactjs - 使用 useHistory、useLocation 挂钩的挂钩调用无效
- file - Webpack html 输出位置
- ssl - 如何使用 JavaFX 14/HTTP2/JDK-8211308 让 JavaFX WebView 继续加载带有自签名证书的页面
- python - scipy.stats.wasserstein_distance 实现
- javascript - 无法使用 python 在 selenium webdriver 中返回 javascript 确认值
- javascript - 显示错误消息并从组件重定向
- python - 分类 - 获取准确的标签值以检查与另一个类的接近程度(Python)
- wordpress - Polylang 无法识别来自重复页面插件的内容
- c# - 在“创建视图”中将 EF 模型传递给视图显示了 @HTML.Editfor 但不是 @HTML.Dropdown 的验证消息?ASP.NET MVC