PHP preg_replace confusion - php

I'm currently scraping code from a website and I like to mark something, so I need to add a new class to a table row if a cell inside the row does match a pattern.
Structure of the table with the key to search for:
<tr><!-- other code --><td>MY KEY</td><!-- other code --></tr>
This should be transformed to the following:
<tr class="mark"><!-- other code --><td>MY KEY</td><!-- other code --></tr>
I tried the following:
preg_replace('#<tr>(.*)<td>MY KEY</td>(.*)</tr>#', '<tr class="mark">$1<td>MY KEY</td>$2</tr>', $html);
This is not working, and I can't get it to work. I tried some other variations with an eye on the PHP documentation about the function, but these were even worse.
EDIT: The problem is, that too many rows get marked even those which doesn't have this <td>MY KEY</td>.
Someone may help me?
Thanks in advance.
Regards

From the previous comments I understand you cannot show the full code because it holds private data.
Then I had the idea to ask you for only one example of an "alien" row (one being selected though not containing your "MY KEY". At this time I realized that in fact "MY KEY" itself might probably hold private data.
And it leads me to this hypothesis: the real content of "MY KEY" might include some regex-significant characters. If so, did you pay attention to carefully escape them?

This should work :
preg_replace('#<tr>(.*)<td>MY KEY</td>(.*)</tr>#', '<tr class="mark">$1<td>MY KEY</td>$2</tr>', $html);

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.

Is there any alternative for * in PHP

I need to explode <TD>, but in code (I need to extract the data from another website that is not mine). There is <TD> with many different attributes, so I can not just write
explode("<TD>",$tableA_data[1]));
And if I write it like
explode("<TD",$tableA_data[1]));
without > mark, it will put that mark in my result. I tried to remove that mark with
str_replace('>',"", $tableA_data);
but it does nothing with result.
CODE:
$tableA_data = explode('<table border="0" cellspacing="0" class="evidenceUpadcuDetailTable">',$result1);
$tableA_data = str_replace('ALIGN="center" nowrap="nowrap">',"", explode("<TD",$tableA_data[1]));
IMO the best option is using a simple HTML parser like Ganon, I used it in a small project and it works nicely and you could also grab any tag you want.
I solved it with substr($foo, 1). Which will basicly remove first character of string $foo. I thing it is not a best way how to do that, but in my case it worked. I will sign Mat´s answer as correct, because it will do work better i thing, thanks guys for help.

Using variable type variables as array names in php

I'm trying to make a dynamic menu in my web, in which only some pages from each section will appear.
The code I wrote was:
$menulist=array();
$menulist[1]='file1%#16';
$menulist[2]='file2%#9';
$menulist[3]='file3%#19';
$menulist[4]='file4%#8';
$menulist[5]='file5%#13';
$menulist[6]='file6%#14';
$menulist[7]='file7%#10';
$menulist[8]='file8%#23';
$menulist[9]='file9%#19';
$menulist[10]='file10%#18';
$menulist[11]='file11%#12';
function actualizaciones($matriz)
{
$linea=explode("%#",$matriz);
echo '<li><a href="first_chunk_of_URL'.$linea[0].'middle_chunk_of_url'.$linea[1].'last_chunk_of_URL">'.${$linea[0]}[$linea[1]].'</li>;
}
echo '<ul>';
array_walk($menulist,'actualizaciones');
echo '</ul>';
Every $linea[0] string is the name of another array (not shown in this code) which contains the text that should be in every possible link corresponding to every key passed by $linea[1].
I must have done something wrong, because the hyperlinks work fine but there's no text showing on them.
use the simple character like below
echo '<li><a href="first_chunk_of_URL'.$linea[0].'middle_chunk_of_url'.$linea[1].'last_chunk_of_URL">'.${$linea[0]}[$linea[1]].'<li>';
and the problem in your code is
.'</li>;
^^^^^
here is the problem it should be
.'</li>';
If I'm reading the question right, you're asking how to use variable variables in PHP.
This can be done using the double-dollar syntax - ie $$linea[0]. See the PHP manual for more info: http://uk.php.net/manual/en/language.variables.variable.php
But if that is what you're doing, I would say you're not writing good code: if variable variables are involved, there's almost always a better way of doing it.
Can't really offer much better assistance here without understanding more about what you're trying to do, but it sounds like you should be using subarrays rather than separate named variables for everything.
Hope that helps.

PHP Double Quotes In Array Key

I know this is really bad practice, but it's already been put in place because it is an existing system. Anyways, I have a table that has an id and a description. What the code does is it creates an array from this table, but to make it easy to search, we made the description they key and the id the value. Now the issue is that the description contains double quotes, example 4"x6", and that is preventing the program from finding the id. Is there any special escape sequence that I can use to get around this issue?
Here is an example of simplified code:
$my_ary = array('4"x6"'=>22, 'test'=>3);
echo $my_ary['4"x6"']; // Does not work
echo $my_ary['test']; // Works
$quote_key = '4"x6"';
echo $my_ary[$quote_key]; // Does not work
Hopefully with that example that will help out my explination.
this is with version 5.2.6-3ubuntu4.2 of PHP
It actually works for me on php 5.3.2
Your code works for me under PHP 5.3.2.

Problem getting text field as string from MySQL with PHP

I'm having this problem that's driving me nuts.
I've got a MySQL database in which there is a table that contains a text field. I am querying the table in PHP and trying to put the content of the text field for each row into a var.
I am doing something like this:
for ($i=0;$i<$nbrows;$i++){
$id = $data[$i]['ID'];
$description = $data[$i]['DESCRIPTION'];
$mystring .= '<div>'.$id.': '.$description.'</div>';
}
DESCRIPTION is my text field.
I'll pass on the details. The $data array is built from mysql_fetch_array($result). I also tried using objects instead, as I use mysql_fetch_object for all my other routines, but there is no change.
Anyway, the problem is this: if I do "echo $description;" then it works. I am getting my text field data as expected. The problem is that I don't want to output it directly, but add it to a concatenated string, and that is not working. What happens in that case is it seems to be taking $description for some kind of array or object. To make the above example work, I have the replace the string with:
$mystring .= '<div>'.$id.': '.$description[0].'</div>';
So in the concatenated string code, if I treat $description as an array, it works, but obviously I am getting only one letter. (it doesn't actually seem to be an array because I can't implode it).
I tried a million things but I just can't make this work unless I use echo, but that is not what I am trying to do.
There is no issue with fields that aren't text.
Thanks for any ideas!
There is nothing visually wrong with the code you pasted, maybe if you could also add the fetching function as well, we might be able to help you further.
Maybe you could post a var_dump of your $data array?
Have you tried $mystring .= "<div> $id : $description </div>";
Ack, well, you know, hours spent on this and then it becomes obvious after I decide to post for help. This is just because of text encoding/escaping and nothing else. I just didn't see well enough where the problem was actually happening.
Thanks for taking the time to read and respond!

Categories