此文本 block 代表作为变量的原始外部源:
// the following characters are not-consistent
text here
// the following characters are consistent
♥ text1
♥ text2
♥ text3
// the following characters are consistent
some more text here
我想删除除“此处的文本”
之外的所有内容,并且我必须在“部分”中执行此操作,因为可能存在其他文本(即我不能删除之后的所有内容“此处文字”
)。
我可以删除“此处还有更多文本”
,但在尝试定位和删除以 ♥ 开头的行时遇到问题(或 ♥,因为它们当前存储在数据库中)。
所以这些是我的变量
// this is the original external source
$part_one = $external_source;
// this is the variable that is not working yet
$part_one_a = ;
// this replaces "some more text here" with nothing
$part_one_b = str_replace("some more text here", " ", $part_one);
// this concatenates the variables $part_one_a and $part_one_b
$final = $part_one_a . $part_one_b;
谢谢。
更新
更具体地说,我需要一种删除文本的方法,如下所示:
♥ link_one: http://link_one_here.com/
♥ link_two: http://link_two_here.com/
♥ link_three: http://link_three_here.com/
更新两个
我已经设法使用数组上的 str_replace 删除所有不需要的项目。
我唯一无法瞄准并因此删除的是♥。
我尝试将其定位为:
♥
(这是当前存储在 数据库)♥
(在我将 htmlentities 应用到字符串之后)
但运气不佳 - ♥ 仍在显示。如果有人能告诉我如何瞄准♥,我将不胜感激,这个问题将得到解决。
谢谢。
最佳答案
如果我理解正确的话,你需要在♥之前的所有文本
,你可以使用strpos
$stopper = '♥';
$string = 'text here
♥ text1
♥ text2
♥ text3
some more text here' ;
$final = trim(substr($string,0,strpos($string, $stopper)));
var_dump($final);
输出
string 'text here' (length=9)
请注意,您的$stopper
也可以是\n
新行
关于php - 如何从 php 中的字符串中删除 ♥(或 ♥)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13332559/