I'm successfully scraping a website to get space separated data off of the page:
$html = file_get_contents("http://www.somewebsite.com");
$scores_doc = new DOMDocument();
$scores_doc->loadHTML($html);
$scores_path = new DOMXPath($scores_doc);
$scores_row = $scores_xpath->query('//td[#class="first"]');
foreach($scores_row as $row){
echo $row->nodeValue . "<br/>";
}
Example output:
23 Crimmons, Bob (CA)
48 Silas, Greg (RI)
82 Huston, Roger (TX)
21 Lester, Terry (NC)
Instead of printing the output using 'echo' I need to split the value into four smaller pieces and into variables (array or otherwise). I know the MySQL side very well, I just don't use PHP day to day. I tried (in place of the 'echo' and after defining it as an array):
$data[] = echo $row->nodeValue;
A sidenote on the used syntax:
If you just want to assign the whole 23 Crimmons, Bob (CA) string as one string to an array. You should use the right syntax.
$data[] = echo $row->nodeValue;
Should be:
$data[] = $row->nodeValue;
Three possible solutions to your problem.
Solution 1: Improve scraping
The best way to scrape those four values seperately would be to query more specifically. You can try to update your xpath query on line:
$scores_xpath->query('//td[#class="first"]');
The query you can use depends on the structure of the page you're scraping.
Solution 2: Splitting string using PHP explode
You could use PHP's explode function to separate the string, but note that will give some problems when there are spaces used in a name.
echo $row->nodeValue . "<br/>";
Can be something like:
// Assuming that $row->nodeValue will have the string `23 Crimmons, Bob (CA)` as it's value
$explodeRow = explode(' ', $row->nodeValue);
/*
* $explodeRow now contains four values.
*
* $explodeRow[0] = "23";
* $explodeRow[1] = "Crimmons,";
* $explodeRow[2] = "Bob";
* $explodeRow[3] = "(CA)";
*/
You can choose to remove the ( and ) characters in $explodeRow[3] with the PHP str_replace, preg_replace or substr function for example.
Solution 3: Splitting string using regular expressions
Alternatively you can decide to fetch the first two numbers first. Then to fetch the last part between (). Then seperate the two remaining values by ,. But this can also generates problems when multiple commas are used.
Example of this solution will be, something like:
preg_match("~^(\d+)~", $row->nodeValue, $number);
$number[1]; # will be 23
preg_match("#\((.*?)\)#", $row->nodeValue, $last);
$last[1]; # will be CA
$middleExp = explode("(", $row->nodeValue, 2);
$middle = substr((strlen($number[1])-1), strlen($row->nodeValue), $middleExp[0]);
$middleExp2 = explode(",", $middle);
$middleL = $middleExp2[0]; # will be Crimmons
$middleR = $middleExp2[1]; # will be Bob
Related
What I would like to do is search a file containing; multiple space delimited words and characters, on multiple lines, using preg_grep, containing a certain string, and store as a variable. I would like to then "for loop" through those matched lines, and search within for yet another string.
For sake of example, assume $c contains all the lines that match variable $a (in an Array?), in the file $o, and returns 9 lines that contain that variable. How
$c = preg_grep("/\b$a\b/", $o);
So I set $max = sizeof($c)
$max = sizeof($c); // In this example contains 9
Here I try to loop through $c to find variable $b, and would like to print that line that matches
for ($i = 0; $i < $max; $i++) {
$st = preg_grep("/\b$b\b/", $c);
echo implode($st, ' ');
}
In the first search if I echo implode($c, ' '), I get all 9 values displayed as one solid string. It seems though using the same technique after my loop, I am not getting the result I want, which is a single line resulting from matching both variables.
Additionally, I know there may be much easier ways to accomplish this, but following this example, Where am I making a mistake(s).
EDIT
If it helps, a sample of the text file:
13 04000 Atlanta city GA 394017 +33.762900 -08.4422592
13 56000 North Atlanta CDP PA 27812 0000000147 +33.862550
Where $c = preg_grep("/\b$a\b/", $o); would match both lines
And ideally, if $b= PA, the second preg_grep would yeild:
13 56000 North Atlanta CDP PA 27812 0000000147 +33.862550
Assuming $o is an array of lines:
$result = preg_grep("/\b$b\b/", preg_grep("/\b$a\b/", $o));
echo implode(" ", $result);
This will give an array of elements from $o that match both $a and $b.
This is fairly confusing, but I'll try to explain as best I can...
I've got a MYSQL table full of strings like this:
{3}12{2}3{5}52
{3}7{2}44
{3}15{2}2{4}132{5}52{6}22
{3}15{2}3{4}168{5}52
Each string is a combination of product options and option values. The numbers inside the { } are the option, for example {3} = Color. The number immediately following each { } number is that option's value, for example 12 = Blue. I've already got the PHP code that knows how to parse these strings and deliver the information correctly, with one exception: For reasons that are probably too convoluted to get into here, the order of the options needs to be 3,4,2,5,6. (To try to modify the rest of the system to accept the current order would be too monumental a task.) It's fine if a particular combination doesn't have all five options, for instance "{3}7{2}44" delivers the expected result. The problem is just with combinations that include option 2 AND option 4-- their order needs to be switched so that any combination that includes both options 2 and 4, the {4} and its corresponding value comes before the {2} and it's corresponding value.
I've tried bringing the column into Excel and using Text to Columns, splitting them up by the "{" and "}" characters and re-ordering the columns, but since not every string yields the same number of columns, the order gets messed up in other ways (like option 5 coming before option 2).
I've also experimented with using PHP to explode each string into an array (which I thought I could then re-sort) using "}" as the delimiter, but I had no luck with that either because then the numbers blend together in other ways that make them unusable.
TL;DR: I have a bunch of strings like the ones quoted above. In every string that contains both a "{2}" and a "{4}", the placement of both of those values needs to be switched, so that the {4} and the number that follows it comes before the {2} and the number that follows it. In other words:
{3}15{2}3{4}168{5}52
needs to become
{3}15{4}168{2}3{5}52
The closest I've been able to come to a solution, in pseudocode, would be something like:
for each string,
if "{4}" is present in this string AND "{2}" is present in this string,
take the "{4}" and every digit that follows it UNTIL you hit another "{" and store that substring as a variable, then remove it from the string.
then, insert that substring back into the string, at a position starting immediately before the "{2}".
I hope that makes some kind of sense...
Is there any way with PHP, Excel, Notepad++, regular expressions, etc., that I can do this? Any help would be insanely appreciated.
EDITED TO ADD: After several people posted solutions, which I tried, I realized that it would be crucial to mention that my host is running PHP 5.2.17, which doesn't seem to allow for usort with custom sorting. If I could upvote everyone's solution (all of which I tried in PHP Sandbox and all of which worked), I would, but my rep is too low.
How would something like this work for you. The first 9 lines just transform your string into an array with each element being an array of the option number and value. The Order establishes an order for the items to appear in and the last does a usort utilizing the order array for positions.
$str = "{3}15{2}2{4}132{5}52{6}22";
$matches = array();
preg_match_all('/\{([0-9]+)\}([0-9]+)/', $str, $matches);
array_shift($matches);
$options = array();
for($x = 0; $x < count($matches[0]); $x++){
$options[] = array($matches[0][$x], $matches[1][$x]);
}
$order = [3,4,2,5,6];
usort($options, function($a, $b) use ($order) {
return array_search($a[0], $order) - array_search($b[0], $order);
});
To get you data back into the required format you would just
$str = "";
foreach($options as $opt){
$str.="{".$opt[0]."}".$opt[1];
}
On of the bonuses here is that when you add a new options type inserting adjusting the order is just a matter of inserting the option number in the correct position of the $order array.
First of all, those options should probably be in a separate table. You're breaking all kinds of normalization rules stuffing those things into a string like that.
But if you really want to parse that out in php, split the string into a key=>value array with something like this:
$options = [];
$pairs = explode('{', $option_string);
foreach($pairs as $pair) {
list($key,$value) = explode('}', $pair);
$options[$key] = $value;
}
I think this will give you:
$options[3]=15;
$options[2]=3;
$options[4]=168;
$options[5]=52;
Another option would be to use some sort of existing serialization (either serialize() or json_encode() in php) instead of rolling your own:
$options_string = json_encode($options);
// store $options_string in db
then
// get $options_string from db
$options = json_decode($options_string);
Here's a neat solution:
$order = array(3, 4, 2, 5, 6);
$string = '{3}15{2}3{4}168{5}52';
$split = preg_split('#\b(?={)#', $string);
usort($split, function($a, $b) use ($order) {
$a = array_search(preg_replace('#^{(\d+)}\d+$#', '$1', $a), $order);
$b = array_search(preg_replace('#^{(\d+)}\d+$#', '$1', $b), $order);
return $a - $b;
});
$split = implode('', $split);
var_dump($split);
Is there a way to take an input like this:
|
testing==one two three
|
setting==more testing
|
and get something like this
array['testing'] = "one two three";
array['setting'] = "more testing"
Right now I'm just exploding the string and setting the array with numbered index, but I'd like the user to be able to enter the items in any order and be able to use the array with keys from the first value.
function get_desc_second_part(&$value) {
list(,$val_b) = explode('==',$value);
$value = trim($val_b);
}
Thanks!
Something like this? The pipes adds some maybe needless complexity (separator could be new lines):
$arr = array();
foreach (explode('|', $str_input) as $line) {
$l = explode('==', trim($line));
if (isset($l[1]))
$arr[$l[0]] = $l[1];
}
print_r($arr);
/*
Array
(
[testing] => one two three
[setting] => more testing
)
*/
If you can change the format of the input to the standard ini format then you could simply call parse_ini_file/parse_ini_string. Your input would need to look like:
testing = one two three
setting = more testing
This would also give you comments (start lines with ;) and sections for free. See http://www.php.net/parse_ini_file
You already do most of the work when you explode on ==, an array index can be manually set to a string and you already separate out the string. Just set your array entries manually,
$myarray = new array();
$myarray[$your_exploded_1st_part_string_here] = exploded_second_part
I have a SQL query which returns a result of one field, so I have the following:
$article_id = $this->item->id;
$authors_default = mysql_query("SELECT multi_authors FROM jos_jxzine_articles WHERE id = '$article_id' LIMIT 1");
$authors_default = mysql_fetch_assoc($authors_default);
echo $authors_default['multi_authors'];
This echos out
128,129
and so on for different queries.
How can I make this into the following
array(128,129)
To then put into a prewritten function?
Cheers
The following code takes that MySQL row and splits it up into pieces using , as the delimiter. It then converts that array of strings to an array of integers.
$authors_arr = explode(',', $authors_default['multi_authors']);
// $authors_arr = array("128", "129");
$authors_arr = array_map('intval', $authors_arr);
// $authors_arr = array(128, 129);
You can then pass that array into a function like so:
myFunction($authors_arr); // Or however you have it setup.
$authors_array = explode(",", $authors_default['multi_authors']);
This will break apart your MySQL result into an array. Since your query pulls a string which is delimited by a comma, the explode() function can be used to separate out the string.
http://php.net/manual/en/function.explode.php
Sorry, this is untested since I have removed PHP from my localhost. If I understand you correctly.
<?php $arr = explode(',', $authors_default['multi_authors']); print_r($arr); ?>
http://php.net/manual/en/function.explode.php
I have a comma delimited string held within a database field that could contain any number of values:
23,45,21,40,67,22
I need to be able to somehow switch two values, so for example I know I need to move 45 one position down the string, so I end up with:
23,21,45,40,67,22
The reason for this is that the numbers all correspond to the IDs held in another database table, and their position in the sting determine the order those items will be printed on screen. Before you ask about database design - I've inherited it and it cannot be changed without significant work to an entire application.
So I've thought about exploding the string, identifying the position of the target number and swapping it with the one next-door, but I'm unsure of how this can be achieved when the total number of values is not known.
Any things? I suspect the solution will be cumbersome, but needs must!!
assuming you need to only move the desired value down one position in the array:
$values = explode(',', $data_string);
$value_to_move = 45;
$value_count = count($values);
for($i=0;$i<$value_count;$i++)
{
if($values[$i] == $value_to_move)
{
if($i < ($value_count-1))
{ // if the value to move is NOT at the end of the list already
$values[$i] = $values[$i+1];
$values[$i+1] = $value_to_move;
$i++;
}
}
}
$new_data_string = implode(',', $values);
I'd just pull them into an array and work with them there. Write the string out in comma-delimited format again, and rewrite that to the DB.
Assuming you know exactly which two values to switch in that list, then explode is the best option:
$array = explode(',', $string)
# find the two values (NOTE: *NO* error handling, what if the values aren't there?)
$index1 = array_search($first_value, $array);
$index2 = array_search($second_value, $array);
# swap them
$temp = $array[$index1];
$array[$index1] = $array[$index2];
$array[$index2] = $temp;
# rebuild the array
$string = implode(',', $array);