如何从 PHP 输出的字符串中删除单词?
我是 PHP 新手,尝试在我的 Joomla 网站上自定义以下 PHP 行:
<? if ($event->venue_id) : ?><?=@service('com://site/component.model.venues')->id($event->component_venue_id)->getItem()->title?><? endif ?>
该行输出地址,但我想通过删除城市和国家/地区来缩短输出的地址。这在 PHP 中可能如何实现?
我一直在尝试用“preg_replace”来做到这一点,但没有取得太大成功。所有地址都是英国地址,所以我尝试过:
<? if ($event->venue_id) : ?><?=@service('com://site/component.model.venues')->id($event->component_venue_id)->getItem(preg_replace('UK','',($event->venue_id))->title?><? endif ?>
感谢您的帮助!
I'm new to PHP and trying to customize the following PHP line on my Joomla site:
<? if ($event->venue_id) : ?><?=@service('com://site/component.model.venues')->id($event->component_venue_id)->getItem()->title?><? endif ?>
The line outputs the address, but I want to shorten the outputted address by removing the city and country. Is this possible some how in PHP?
I have been trying to do it with 'preg_replace' but not having much success. All the addresses are UK address so I tried:
<? if ($event->venue_id) : ?><?=@service('com://site/component.model.venues')->id($event->component_venue_id)->getItem(preg_replace('UK','',($event->venue_id))->title?><? endif ?>
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只想删除
, UK
部分,则不需要 preg_replace,你可以做一个常规的str_replace:改变这个:
到这个:
这个将用空字符串替换
, UK
。因此,它将把这样的地址:变成这样:
如果您还想删除字符串
London,
,您可以添加另一个 str_replace:这将导致该地址变为:
If you just want to remove the
, UK
part, you don't need preg_replace, you can just do a regular str_replace:Change this:
To this:
This will replace
, UK
with an empty string. So it will turn an address like this:Into this:
If you also want to remove, for example, the string
London,
, you can just add another str_replace:This would result in that address becoming: