如何从 PHP 输出的字符串中删除单词?

发布于 2025-01-07 10:00:45 字数 623 浏览 2 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑕疵 2025-01-14 10:00:45

如果您只想删除 , UK 部分,则不需要 preg_replace,你可以做一个常规的str_replace:

改变这个:

@service('com://site/component.model.venues')->id($event->component_venue_id)->getItem()->title

到这个:

str_replace(', UK', '', @service('com://site/component.model.venues')->id($event->component_venue_id)->getItem()->title)

这个将用空字符串替换 , UK。因此,它将把这样的地址:

11 Water Square, London, Greater London A12 34Z, UK

变成这样:

11 Water Square, London, Greater London A12 34Z

如果您还想删除字符串 London,,您可以添加另一个 str_replace:

str_replace('London, ', '', str_replace(', UK', '', @service('com://site/component.model.venues')->id($event->component_venue_id)->getItem()->title))

这将导致该地址变为:

11 Water Square, Greater London A12 34Z

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:

@service('com://site/component.model.venues')->id($event->component_venue_id)->getItem()->title

To this:

str_replace(', UK', '', @service('com://site/component.model.venues')->id($event->component_venue_id)->getItem()->title)

This will replace , UK with an empty string. So it will turn an address like this:

11 Water Square, London, Greater London A12 34Z, UK

Into this:

11 Water Square, London, Greater London A12 34Z

If you also want to remove, for example, the string London,, you can just add another str_replace:

str_replace('London, ', '', str_replace(', UK', '', @service('com://site/component.model.venues')->id($event->component_venue_id)->getItem()->title))

This would result in that address becoming:

11 Water Square, Greater London A12 34Z
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文