Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

escaping - Php remove single quote from string

Breaking my head here. I'm getting data from a website.

$page = file_get_contents('https://somepage.com/example');

I use the following to search for data I want to filter out the information:

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

The value I'm getting is : $ 9'858'470, which is what I want. I would expect this to be a string value. Since I want to insert this data into a database I would like to remove the $ sign and the single quotes. I'm trying it this way:

$replacechars = array ("'", "$");
echo $string = str_replace($replacechars, "", $match[1]);

This returns: 9'858'470

I don't understand why I still see the single quotes.

When I just put

$string2 = "9'858'470";
$replacechars = array ("'", "$");
echo $string2 = str_replace($replacechars, "", $string2);

It works. Is there a problem with the value filtered from the webpage?

Updated with my code.

<?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);


?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't think escaping the single quote would work in str_replace where you're using double quotes to wrap the needle values - I'd be tempted as you want just the numbers to go with:

$string = preg_replace("/[^0-9]/", "", $match[1]);

As a more reliable approach.

Alternatively if you're confident that the single quote and $ are the only characters to strip just remove your in the array so you're looking for just "'" rather than "'"

UPDATE:

Try removing htmlentities & any tags first to be safe:

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

UPDATE 2:

The below code will work for you and strips the HTML entity which was representing the quote mark.

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);

For future similar issues you can see the actual content, hidden content and all when debugging using json_encode / htmlspecialchars. e.g.:

echo json_encode(htmlspecialchars($match[1]));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.7k users

...