I'm trying to get the last 9 characters of $span.
$span = "";
foreach($html->find('span') as $element1){
if (strpos($element1->outertext, 'kcal') !== false){
$span .= $element1->outertext.'<br>';
}
}
echo substr($span,-9);
It just show me white page, any suggestions?
Edit:
When i debug with var_dump($span) it shows exactly the following:
string(761) " 1 Porsiyon (Orta) AnçuezSardalya Salatası 319 kcal 1 Su Bardağı Ayran (Yağsız) 41 kcal 1 Su Bardağı Anne Sütü 138 kcal 1 Porsiyon (Orta) Amasya Yöresine Özgü Keşkek 728 kcal 1 Porsiyon (Orta) Anne Kurabiyesi 504 kcal "
use trim() to remove white spaces
so you can write
echo substr(trim($span),-9);
Related
I have a string like this
aaa ~120 Sek. 53 Sek. ~~ bbb asdfasf aasdf asdfasdf ~600 Sek.~~ ccc ~60 Sek. 43 Sek. ~~ ddd ~240 Sek. 55 Sek. ~~
I have to add up all the xxx Sek. (which are just seconds).
Any good idea?
Regards Bruno
One way to do with regex look ahead, so you can get all the digits followed by Sek word. If you want to do any filtering on a matched result like 3 digits or something else then you can use strlen() function.
$re = '/\d+(?= Sek)/';
$str = 'aaa ~120 Sek. 53 Sek. ~~ bbb asdfasf aasdf asdfasdf ~600 Sek. ~~ ccc ~60 Sek. 43 Sek. ~~ ddd ~240 Sek. 55 Sek. ~~';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
#print_r($matches);
$sum = 0;
foreach ($matches as $item) {
$sum += $item[0];
}
echo $sum;
Edit: Calculate sum alternatively,
$sum = array_reduce($matches, function(&$result, $item) {
return $result + $item[0];
}, 0);
OR
$sum= array_sum(array_column($matches,0));
Working demo: https://3v4l.org/iQp0q
AbraCadaver gave me a link to phpsandbox. The code below works on http://sandbox.onlinephpfunctions.com/ .
Why it does not work in my computer? I have wampstack-5.6.20-0, php version 5.6.20 and check oce on Opera, Firefox, Explorer and Chrome.
I have a string, which i would like to split to pieces in order to join them with a glue string OR preg_replace the split_substring(aaaa) with a glue_string (jsonString) :
$tmpStr = '<br><div id="10"><p>Displaying 1-5 of 11 results. <br> <span class="disabled">«</span> <span class="disabled">‹</span> Page 1 of 3 pages› »<br>All pages: 1 2 3 </p></div><br> ';
$tmpArr = explode("aaaa", $tmpStr);
print_r("<br> 94 EventSearchCOntroller tmpArr =<pre>"); print_r($tmpArr);
Result is:
Array
(
[0] =>
107 PaginatorTrait pagHTML =
Displaying 1-5 of 11 results.
« ‹ Page 1 of 3 pages› »
All pages: 1 2 3
)
$tmpStr2=preg_replace( "/aaaa/", "jsonString", $tmpStr );
$tmpStr2=preg_replace( "/aaaa/", "jsonString", $tmpStr, 100 );
print_r( "<br> 97 EventSearchCOntroller tmpStr2 =".$tmpStr2 );
//Result is in both cases (using 100 limit, and without a limit):
97 EventSearchCOntroller tmpStr2 =
If I print the results, the string is not splitted. Where is the reason?
Just try this and see the output:
$tmpStr = '<br><div id="10"><p>Displaying 1-5 of 11 results. <br> <span class="disabled">«</span> <span class="disabled">‹</span> Page 1 of 3 pages› »<br>All pages: 1 2 3 </p></div><br> ';
$tmpArr = explode("aaaa", $tmpStr);
echo "<textarea rows='20' cols='120'>";
print_r($tmpArr);
echo "</textarea>";
im doing some data scraping ... basically i'm getting some webpage using curl , extract the data and check my database to see if they exist in my db .
so i was been looking for Beijing Guoan (Chn) in a webpage source code and i couldn't find it , but it was there and i could see it in the browser .
$result = phpQuery::newDocument( file_get_contents('www.site.com/page'), 'text/html');
foreach($result->find('td.table-participant-teams') as $t )
{
list( $host , $guest ) = explode( ' - ' , pq($t)->text());
echo $host.' == Beijing Guoan (Chn) ==> ';
echo $host == 'Beijing Guoan (Chn)' ? ' found it ' : ' false ';
}
result :
Beijing Guoan (Chn) == Beijing Guoan (Chn) ==> false
i did a strlen($host) and i found $host was 20 charchter while Beijing Guoan (Chn) has 19 .... basically there is hidden charachter in $host
so i've added
for($i = 0 ; $i < strlen($host) ; $i++)
{
echo $i.' - '.$host[$i];
echo '<br />';
}
and i got
0 - B
1 - e
2 - i
3 - j
4 - i
5 - n
6 - g
7 -
8 - G
9 - u
10 - o
11 - a
12 - n
13 -
14 -
15 - (
16 - C
17 - h
18 - n
19 - )
as you can see in 13,14 i got 2 spaces , but when i print out $host i only have 1 ! and that's what cuzing all the trouble
so whay there is a extra space in my $host but it wont show when i print it out on the screen and how can i get rid of it ?
please note that i don't want to just remove that extra space from this specific string , there might be other cases with different char-length , iwant a solution that works on all of them
HTML renders multiple consecutive space as one. If you view the source you will see the actual data.
To replace multiple consecutive white space you can use the following
echo preg_replace('/ +/', ' ', 'he llo test');
I was just trying to do a simple sorting algorithm on a matrix that I read from a matrix.txt file and append the sorted matrix back to the file.
The problem is that undesired new lines are written to the text file. I also tried in parallel to echo the same things I am writing in the text file, but the echo prints everything okay.
// .. reading the file and sorting the matrix ..
// Write the sorted matrix back to the text file
$handle = #fopen("matrix.txt", "a");
if ($handle) {
fwrite($handle, PHP_EOL . PHP_EOL . "Sorted matrix:" . PHP_EOL);
for ($i = 0; $i < $n; $i++) {
for ($j = 0; $j < $m; $j++) {
echo $matrix[$i][$j] . " ";
fwrite($handle, $matrix[$i][$j] . " ");
}
fwrite($handle, PHP_EOL);
echo "<br>";
}
fclose($handle);
}
matrix.txt file contents:
1 2 5 2 5 8 12 323 1 4
8 32 2 1 3 82 2 8 4 2
1 2 5 2 5 8 12 323 1 4
8 32 2 1 3 82 2 8 4 2
In the web browser it echoes the matrix nicely sorted, each row by itself; however, in the text file, the following is appended:
Matrix sorted using selection sort:
1 1 2 2 4
5 5 8 12 323
1 2 2 2
3 4 8 8 32 82
1 1 2 2 4
5 5 8 12 323
1 2 2 2 3 4 8 8 32 82
Any clues what could cause this? Thanks in advance !
The problem isn't in the code you posted; it's in the input matrix you provided. Notice that every extra newline corresponds to the item which used to be at the end of the row, except for the last row. That's because the final newline from each row is being included when you read the line, and explode (which I imagine you're using) doesn't know to remove it. You could simply trim the lines before exploding to fix this, or specifically remove \r and \n characters.
I was given a file that contains something similar to this kind of structure:
12345 ABC 100M 001 2.0 ABC 1010 4510 A01 451 Apple, Johnny A 150
12345 ABC 100M 011 2.0 ABC 1010 4510 A01 451 Apple, Johnny A 80
12345 ABC 100 011 2.0 ABC 1010 4510 A01 451 Apple, Johnny A 80
I need to grab the following sections from this file:
Group together the third column (ie. 100M) if they are similar
Add together the fourth column (if they are in the same group as the third column)
Add up the last column depending on the fourth column
I've managed to do the following:
$List1 = array();
$grab = fopen("file.txt", "r") or die("Can't open file");
$check = fgets($grab);
while(!feof($grab)) {
if (ereg("^[[:digit:]]{5} +ABC +([[:digit:]]{3}[[:alpha:]]?)+ ([[:digit:]]{3})",
$check, $output)) {
if (!in_array($output[1], $List1)) {
array_push($List1, $output[1]);
}
if (!in_array($output[2], $List1)) {
array_push($List1, $output[2]);
}
}
$check = fgets($grab);
}
fclose($grab);
foreach ($List1 as $list) {
print "$list <br/>";
}
I have managed to somehow group together the third column.
The fourth column is being displayed, but I'm not sure how to group it together into the third column if it's under the same group.
And I'm not sure how to easily grab the last bit in the file/array.
Is there a shortcut to getting the last in a file and adding them up?
Thanks in advance for anyone who can help me.
This should do it:
$string = '12345 ABC 100M 001 2.0 ABC 1010 4510 A01 451 Apple, Johnny A 150
12345 ABC 100M 011 2.0 ABC 1010 4510 A01 451 Apple, Johnny A 80
12345 ABC 100 011 2.0 ABC 1010 4510 A01 451 Apple, Johnny A 80';
$third = array();
$fourth = array();
foreach (explode("\n", $string) as $line)
{
// Skip empty lines.
if (empty($line))
continue;
// Clean up any excessive white space.
$line = trim(preg_replace('~[\s]{2,}~', ' ', $line));
$info = explode(' ', $line);
if (!isset($third[$info[2]]))
$third[$info[2]] = array();
$third[$info[2]][] = $info;
if (!isset($fourth[$info[3]]))
$fourth[$info[3]] = 0;
$fourth[$info[3]] += (int) end($info);
}
print_r(array(
'third' => $third,
'fourth' => $fourth,
));