删除字符串后的文本 [英] Removing text after string

查看:26
本文介绍了删除字符串后的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的链接

<img src='http://www.example.org/images/post_images/2400.jpg' /><div>© Ham Sammich</div></div>

我想删除 /> 之后的文本,从而建立链接..

I want to remove the text after the /> thus making the link..

<img src='http://www.example.org/images/post_images/2400.jpg' />

我已经尝试在最后使用 strstr() 函数,但是我的 vps 没有转到 php5.3 的选项,因此没有成功.

I've tried using the strstr() function on the end, but my vps does not have the option of going to php5.3 thus making that not a go.

这是我的代码,由于 5.3 不在那里,所以它不起作用..

Here is my code, which does not work because of 5.3 not being on there..

$imgTwo = strstr($img, ">", 1);
$img = $imgTwo . ">";

有什么好的替代方法可以代替按照我想要的方式完成这项工作?

Whats a good substitute for getting this done the way I want?

推荐答案

对于@JonathanRich 给出的方案,有替代方案,并且不涉及正则表达式.

There is alternative solution to the one given by @JonathanRich and it does not involve regular expressions.

$result = array_shift(explode('/>', $string)).'/>';

其中 $string 是您的字符串,$result 是处理的结果.

where $string is your string and $result is the result of processing.

什么意思?它说在/>"上拆分字符串并在第一次出现之前返回部分,然后向它添加/>"并保存为$result".这意味着它完全符合您的要求:)

What it means? It says "split the string on "/>" and return part before its first occurence, then add "/>" to it and save as $result". Which means it does exactly what you wanted :)

这是实际工作的演示:codepad.org/0TTCypA5

还有上述替代解决方案的替代解决方案 :)

$result = substr($string, 0, strpos($string, '/>')+2);

演示在这里:codepad.org/ifyJiaPB.这个说:找到字符串中/>"第一次出现的位置并返回之前的所有内容,包括/>"本身".

the demonstration of which is here: codepad.org/ifyJiaPB. This one says: "find the position of first occurence of "/>" within the string and return everything before, including "/>" itself".

这篇关于删除字符串后的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆