Php从字符串中删除单引号

Php从字符串中删除单引号,php,escaping,single-quotes,Php,Escaping,Single Quotes,我的头在这里折断了。 我从一个网站上获取数据 $page = file_get_contents('https://somepage.com/example'); 我使用以下方法搜索我要筛选出的信息: preg_match('/class="accent no-margin-bottom inline">(.*?)</', $page, $match); 返回:9'858'470 我不明白为什么我仍然看到单引号 当我刚把 $string2 = "9'

我的头在这里折断了。 我从一个网站上获取数据

$page = file_get_contents('https://somepage.com/example');
我使用以下方法搜索我要筛选出的信息:

preg_match('/class="accent no-margin-bottom inline">(.*?)</', $page, $match);
返回:9'858'470

我不明白为什么我仍然看到单引号

当我刚把

$string2 = "9'858'470";
$replacechars = array ("'", "$");
echo $string2 = str_replace($replacechars, "", $string2);
它起作用了。从网页中筛选的值是否有问题

用我的代码更新

<?php
require 'simple_html_dom.php';
$html = file_get_html('https://swissborg.com/chsb-overview');
$replacechars = array ("'", "$");
preg_match('/class="accent no-margin-bottom inline">(.*?)</', $html, $match);
//echo "<BR>";
var_dump($match[1]);
echo "<BR>";
$string = preg_replace("/[^0-9]/", "", strip_tags(html_entity_decode($match[1])));
var_dump($string);


?>

我认为在str_replace中,如果您使用双引号来包装指针值,那么转义单引号将不起作用-我会受到诱惑,因为您只希望数字与之匹配:

$string = preg_replace("/[^0-9]/", "", $match[1]);
作为一种更可靠的方法

或者,如果您确信单引号和$是唯一要删除的字符,只需删除数组中的\即可,因此您只需查找“'”而不是“'”

更新:

为安全起见,请首先尝试删除htmlentities和任何标记:

$string = preg_replace("/[^0-9]/", "", strip_tags(html_entity_decode($match[1])));
更新2:

下面的代码将适用于您并剥离表示引号的HTML实体

require 'simple_html_dom.php';
$html = file_get_html('https://swissborg.com/chsb-overview');
$replacechars = array ("'", "$");
preg_match('/class="accent no-margin-bottom inline">(.*?)</', $html, $match);
//echo "<BR>";
echo "<BR>";
$string = preg_replace("/&#?[a-z0-9]+;/i","",$match[1]);
var_dump($string);

我认为,在str_replace中,如果使用双引号来包装指针值,则不需要使用单引号-我会受到诱惑,因为您只需要数字:

$string = preg_replace("/[^0-9]/", "", $match[1]);
作为一种更可靠的方法

或者,如果您确信单引号和$是唯一要删除的字符,只需删除数组中的\即可,因此您只需查找“'”而不是“'”

更新:

为安全起见,请首先尝试删除htmlentities和任何标记:

$string = preg_replace("/[^0-9]/", "", strip_tags(html_entity_decode($match[1])));
更新2:

下面的代码将适用于您并剥离表示引号的HTML实体

require 'simple_html_dom.php';
$html = file_get_html('https://swissborg.com/chsb-overview');
$replacechars = array ("'", "$");
preg_match('/class="accent no-margin-bottom inline">(.*?)</', $html, $match);
//echo "<BR>";
echo "<BR>";
$string = preg_replace("/&#?[a-z0-9]+;/i","",$match[1]);
var_dump($string);
这个

不是这个

$replacechars = array ("\'", "$");
这个

不是这个

$replacechars = array ("\'", "$");

您不需要在双引号字符串中转义单引号,只需使用
array(“'”,“$”)即可旁注:不要使用正则表达式解析HTML。有一天,响应将出现类似于
class=“accent no margin bottom inline”id=“42”>
的内容,您的模式将不再有效。这同样适用于foo-bar hello-worldarray(“'”,“$”);不起作用。单引号不会被删除。您不需要在双引号字符串中转义单引号,请尝试使用
array(“'”,“$”旁注:不要使用正则表达式解析HTML。有一天,响应将出现类似于
class=“accent no margin bottom inline”id=“42”>
的内容,您的模式将不再有效。这同样适用于foo-bar hello-worldarray(“'”,“$”);不起作用。单引号不会被删除。:)建议的解决方案不起作用。从网页检索到的字符串似乎有点奇怪。:)建议的解决方案不起作用。从网页中检索到的字符串似乎有些奇怪。感谢您的回答,但是,现在单引号已替换为27。现在我看到的是92785827470,而不是9'858'470,如果您使用var_dump($match[1]),那么输出是什么?字符串(20)“9'858'470”啊,我认为您看到的字符串必须是HTML编码的。现在更新我的答案…非常感谢。谢谢你的回答,但是,现在单引号被27取代。现在我看到的是92785827470,而不是9'858'470,如果您使用var_dump($match[1]),那么输出是什么?字符串(20)“9'858'470”啊,我认为您看到的字符串必须是HTML编码的。现在更新我的答案…非常感谢。