0

In my two-dimensional array, the value of array's key [1] is empty, and the value of key name [4], [5], [6], [7], [8] suppose is empty but it show there have String (2) within the arrays values. Well, I think that 2 string maybe is two white spaces, and I tried a variety of methods such as preg_replace, str_replace, trim, array_map, array_filter are not delete or remove the two spaces, all of the functions that i used are not detected that two spaces as a white spaces. Besides that, I also try to use preg_replace, str_replace to replace the two spaces with other string. But it is only the array values and key name [1] have changing ... I dont know why that 2 strings is not recognized as a white space. In addition, I also tried to retype the same arrays in the new php file and debug it, I replace the array values of keys [4], [5], [6], [7], [8] with two spaces. However, the function able to detect that all is a white spaces but the following arrays i post at here the two spaces instead of spaces, obviously they are the same things.

 [1]=>
  array(9) {
    [0]=>
    string(5) "Johor"
    [1]=>
    string(0) ""
    [2]=>
    string(10) "KotaTinggi"
    [3]=>
    string(3) "29*"
    [4]=>
    string(2) " "
    [5]=>
    string(2) " "
    [6]=>
    string(2) " "
    [7]=>
    string(2) " "
    [8]=>
    string(2) " "
  }

Here is the code that i used. The arrays is scraping with curl from a website.

<?php
$ch = curl_init("http://apims.doe.gov.my/v2/hour3_2017-01-31.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($page);
libxml_clear_errors();
$xpath = new DOMXpath($dom);
$data = array();

$table_rows = $xpath->query('//tr');
foreach ($table_rows as $row => $tr) {
    foreach ($tr->childNodes as $td) {
        $data[$row][] = preg_replace('/\s+/', '', trim($td->nodeValue,"  "));
    }
}
var_dump($data);
 ?>

I also try with this.But all of these are not working. Any pro can help me? or give me some solution.

 $data[$row][] = preg_replace('/\s+/', '', str_replace(' ','',$td->nodeValue));
7
  • I see special characters in fields like Â. Commented Jan 31, 2017 at 4:07
  • @Suchit How can i remove it? i use preg_replace with \s is able to remove it. Commented Jan 31, 2017 at 4:09
  • if you are expecting only numder and characters you can use: preg_replace("/[^a-zA-Z0-9]/", "", $td->nodeValue); Commented Jan 31, 2017 at 4:15
  • @Suchit it work, thank you so much!!! Commented Jan 31, 2017 at 4:22
  • the above will remove spaces also try the one which i have added in the answer. Commented Jan 31, 2017 at 4:23

2 Answers 2

1

If you are expecting characters and numbers you can use this(keeping * in the string):

preg_replace("/[^a-zA-Z0-9\*]/", "", $td->nodeValue);// this will remove spaces from a proper string also.

or

$data[$row][] = preg_replace("/[^ \w\*]+/", "", $td->nodeValue);// this works better and does not remove spaces
Sign up to request clarification or add additional context in comments.

Comments

0

If you take a look at the website, the space is actually an &nbsp; string, so the following should do it:

$data[$row][] = preg_replace('/\&nbsp;/', '', trim($td->nodeValue,"  "));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.