如何删除♥;(或â;™;¥;)来自php中的字符串?
此文本块表示作为变量的原始外部源:如何删除♥;(或â;™;¥;)来自php中的字符串?,php,string,replace,Php,String,Replace,此文本块表示作为变量的原始外部源: // 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 我想删除除了“此处文本”之外的所有内容,我必须在“部分”中删除,因为可能存在其他文本(
// 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
新行解决方案是:
在数组上执行一个操作,以定位并删除文本的特定部分李>
创建正则表达式并定义可接受的值
对不可接受的值进行测试李>
这导致显示的所有所需值都没有不可接受字符的实例
最后两个步骤的代码如下:
// this is the original external source
$part_one = $external_source;
// create a regex
$regex
= '~'
. '['
. '^'
. 'A-Z'
. '0-9'
. ' '
. '!_:/,.#$-'
. ']'
. '~'
. 'i'
;
// clean the string
$part_one_cleaned = preg_replace($regex, NULL, $part_one);
您可能希望跳过每个非ASCII unicode字符,但这可能太强了……您的描述似乎相互矛盾:您说“要删除此处除文本以外的所有内容”
,但可能还需要保留其他文本?如何确定是否需要保留或丢弃某些特定文本?在上面的示例中,我希望删除除此处的文本以外的所有部分
,但此变量中可能存在其他元素(因为它是外部和不一致的源)这就是为什么我必须能够针对上面示例中的每个元素并删除它(以便可能存在的其他文本仍然显示)。我可以成功删除除以开头的行以外的所有元素♥ (或-™¥因为它们当前存储在数据库中)。因此,我的问题是如何删除这些行。将字符集设置为utf-8?感谢您的回复@Chris,您能否在获取数据的表单页面和在标题区域显示数据库数据的页面上提供有关如何执行此操作的更多信息,搜索将字符集设置为utf-8 php
立即引导我了解几个方向,感谢您的澄清。非常感谢您的建议,但是我需要以特定的方式删除元素,因为在以开头的行之前或之后可能会出现其他文本♥ 这也需要包括在内。我认为主要的问题是试图以♥ 当我的代码和文本编辑器将该符号转换为问号?
时,如果我使用™¥
内容未被删除。您说过您想要这里的文本
而不是text1
,text2
,text3
。。。。。。由于字符集设置,它会显示?