Simple_HTML_DOM parse 'find' value to Integer - php

I'm studying Simple_HTML_DOM and I found a problem where I'm working on from 2 hours without finding a solution :-(
I've the following code:
$num = $html->find('span.pageNum');
This code get number of pages on my blog and it works. GREAT.
If I do
echo $num[0];
I see the number of pages on screen, but if I try to do something like:
echo ($num[0]+2);
for example, It doesen't work like an INTEGER and it return me the following output:
3
So, I need to make the "find" to an integer and work for math operations but I tried everything and nothing works (like cast with (int)$num[0] and much more).
Can anyone please help me? I don't know what to do more...I really tried everything!! :-(
Thanks in advance!!

FOUND THE SOLUTION!!
I used:
$pgnum = filter_var($num[0], FILTER_SANITIZE_NUMBER_INT);
worked GREAT!! Thanks :-)

Related

PHP Array in Google Maps

I'm pulling google map data into a php array from MySQL. I have used this code as a base:
https://github.com/rajkavinchetty/Google-Maps-API-with-PHP-MySQL/blob/master/index.php
It almost works perfectly. The lat/long data is pulled from the array and sets-up the markers.
...
$locations[]=array( 'name'=>$name, 'lat'=>$latitude, 'lng'=>$longitude, 'lnk'=>$link );
...
But I'm having problem with the Title/Content of the infowindow.
With the base example I'm using, the content is formatted as a link:
var locations = [
<?php for($i=0;$i<sizeof($locations);$i++){ $j=$i+1;?>
[
'AMC Service',
'<p><?php echo $locations[0]['name'];?></p>',
<?php echo $locations[$i]['lat'];?>,
<?php echo $locations[$i]['lng'];?>,
0
]<?php if($j!=sizeof($locations))echo ","; }?>
];
but the variable pulls only the first link and name from the array, it does not go through the loop. I have tried many different variables, such as
<?php echo $locations[$i]['name'];?>
but this gives an error.
I'm not an expert in either php arrays or loop or java, so I'm fumbling around for clues. I've looked through all of the related questions here for help, and also consulted the Google stack as well.
Any help would be appreciated.
UPDATE:
The first three replies were useful, thank you, but no there yet.
After the comment from Andy, and encouragement from Divyamohan that the original solution should have worked, I realised that there must be some json formatting problem, so I tried:
<?php echo json_encode($locations[$i]['name']);?>
This worked and now I'm able to have these loop through.
But still trying to get this to work as an href link. I tried to solve with backslashes as Hardik and David observed, but still this does not work. Half way there.
'<p><?=$locations[$i]["name"]?></p>'
Just Replace this in your code and it should work.
You need to change this line
'<p><?php echo $locations[0]['name'];?></p>'
to
'<p><?php echo $locations[0][\'name\'];?></p>'
You need to add backslash ( \ ) character to escape quote from string literals.
So finally I found the solution to the problem. Thanks to the comments from everyone that helped me to look at it another way.
In the end, the problem was not with the code. The problem was the data. This should have been obvious. In the links column I had URLs with slashes and text in the names column included apostrophes and foreign characters. These combined were throwing off the json errors. I cleaned all the data and the modified code works perfectly.
<p><?=$locations[$i]['name']?></p>
Many thanks again for the comments.

How to deal with 'µg' in PHP?

So I am having problems with the symbol µg in PHP. The symbol is inside an array of data but I can't seem to match it with the following:
if($value == 'µg') {
echo 'blah';
}
Does anyone happen to have a work around for this? I'm assuming PHP saves it as a different type then what I am comparing it to because it never echo's the 'blah' above. I have searched around for a while now and cannot find anything to help me. Thanks!
Okay so even with the help of some people I haven't found the answer.
The best I came up with that actually works is:
substr('µg', 1,7) == 'micro;g'
That's the only way I found out how to parse the data. If you put the & in front of the micro; you get the µ. A dirty fix but if anyone has a way around please let me know. Thanks!

How to extract string from another string between the Nth and N+1th 'breakword' in php?

My example code is given below.
$a= "I think there are many people who can help in my difficulty. breakword Thank you in advance. brwakword If i got this solution I will be happy. Breakword I have already searched in google about it I did not get it's solution. Breakword If you have any link of solution please help me. breakword Thank you again!";
From the above string I want to get the sentence between 2nd and 3rd 'breakword' word.
It means I want to extract 'If i got this solution I will be happy.'.
If you have any solution please help me. Thank you in advance
You can split your string into smaller parts using explode() and get the "Nth" part of the array.
$sentences = explode('breakword', $a);
echo $sentences[2];
PHP > 5.4 will support this too :
echo explode('breakword', $a)[2];
a similar approach but intead of using explode you can also use preg_split
supports (PHP 4, PHP 5)
preg_split('/breakword/', $a);
for the second answer you can use
$dummy=array();
preg_match_all('/breakword/', $a,$dummy)

php xml read access xml object always 0

This code behaves different on my system as on for example codepad and i don't understand why.
http://codepad.org/jBmXRgyY
The output on my system is only 0.
Does anyone know where to look?
found a workaround by myself.
foreach($chstr->channel->children() as $test)
echo (string)$test."\n";
works fine for me.

Simple if statement not working in wordpress echo is working

I am hoping that someone can help me, I am writing a simple if statement inside WordPress but for some reason it does not seem to execute the way I want it to work.
$g_map = get_the_id().(get_post_meta($post->ID, '_et_business_g_pagetype', true));
if ('map' == $g_map) {
echo "<h1>This is the map page</h1>".$g_map;
}
The if statement is to execute when a Custom Post Meta Field = map
If I execute this line echo get_the_id().(get_post_meta($post->ID, '_et_business_g_pagetype', true)); outside the if statement is does show that the value is "map"
So "map should equal map" and the echo should run as intended, but I cannot understand why it is not...
Any ideas why this maybe happening and how I can fix it will be appreciated.
Do a var_dump($_map); and see what returns it might be a case issue or is doesn't return map. oh and if you want to refactor it to be a little better use === then it won't type cast it but meh it is a string so not much difference.
I have placed the get_the_id() to see if I have the right post! - so infact map did not equal map... it was map = (postnumber)map and this is why the if statement did not work! - Thanks a million for pointing me in the right direction! It is now solved!

Categories