php - 如何从 php 中的字符串中删除 ♥(或 ♥)?

标签 php string replace

此文本 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/

相关文章:

php - MySQL - 如果两个加数相同,则增加总和值

c# - 改变字符串的中间位置

php - 如何将php zip添加到服务器,我在谷歌云平台上使用wordpress

php - 如何在 PHP 中记录对函数的所有调用? (邮件() 函数)

c++ - 如何将包含多个数字的字符串转换为整数并将其存储在 vector 中

ruby - 在不使用 ruby​​ 中的 eval 的情况下将字符串转换为类名?

java - 制作密码 key validator ,其中一个输出应显示 : Reports ALL The Rules That The Key Failed

Mysql - 更换麻烦

linux - 用 bash 脚本中的 sed 替换另一个文件中的字符串

php - 我们如何根据 php 和 mysql 中关键字的出现对搜索结果进行排序