have this text
"Number":"X0011","Name":"aaaaaaaaaaaaaa","Adress":"12345","Phone":"56976585","Number":"X0044","Name":"bbbbbbbbbbbbbbbbbbb","Adress":"786487647","Phone":"34214234",.......
And want to return with PHP something like this (just names and codes, and ignore all others fields)
Number:X0011
Name:aaaaaaaaaaaaaa
Number:X0044
Name:bbbbbbbbbbbbbbbbbbb
....
Thanks in advance
You can do that with the explode function
========
explode function
$mystring = ""Number":"X0011","Name":"aaaaaaaaaaaaaa","Adress":"12345","Phone":"56976585";
$pieces = explode(",", $mystring);
echo $pieces[0]; // Number:X0011
echo $pieces[1]; // Name:aaaaaaa
The following code will allow you to cycle through all of the items and get at the individual values $temp_str[1] will contain the actual value of that item. It also means that you can keep adding info to the of the string and this code will parse it.
$str = '"Number":"X0011","Name":"aaaaaaaaaaaaaa","Adress":"12345","Phone":"56976585"';
$str = str_replace('"',"",$str); // remove the double quotes
$pieces = explode(",", $str);
foreach($pieces as $piece){
$temp_str = explode(',',$piece);
echo $temp_str[0]; // 'Number:'X0011 // gives the heading
echo $temp_str[1]; // Name:'aaaaaaa' // gives the value
echo "<br />";
}
Related
I am getting data from a page like this:
<?php $speaker = $page->uri();
$speakerEvents = page('program')->grandchildren()->filter(function($child) use($speaker) {
$speakers = $child->speaker()->toStructure();
return $speakers->findBy('selectspeaker', $speaker);
});
echo $speakerEvents;
?>
It's output is:
"page/page/pageIWant
page/page/pageIWant"
The Result I want is
pageIWant
pageIWant
I tried to get the last name with
echo basename($speakerEvents);
But I only get one of the last pageIWant
How do I get the last pages without removing the first URL?
Use the explode method to get array of URL
<?php
$link = $_SERVER['PHP_SELF'];
$link_array = explode('/',$link);
echo $page = end($link_array);
?>
If your output is
page/page/pageIWant
page/page/pageIWant
<?php
$text = "page/page/pageIWant";
$text_arr = explode('/',$text);
echo $page = end($text_arr);
// this will print: pageIWant
?>
Your string has a line break as path separator. You need to split lines by either \r\n (CR-LF), \n, or \r which can be done with preg_split. If you can ensure one of the formats, you can also use the simple explode function.
foreach (preg_split('~(?:\r?\n|\r)~u', $speakerEvents) as $path)
echo basename($path) , PHP_EOL;
As AbraCadaver noted, you can add the flag PREG_SPLIT_NO_EMPTY to preg_split if you do not want empty lines to be handled.
If the double qoutes you show in the question are part of the string, you need further more to trim the string:
foreach (preg_split('~(?:\r?\n|\r)~u', trim($speakerEvents, '"')) as $path)
echo basename($path) , PHP_EOL;
You can explode the string e.g.
$array = explode('/', 'page/page/pageIWant');
Then you can retrieve it using the index like so:
$array[(count($array) - 1)];
Thanks for viewing, just need some advice if this is the proper way of taking a string full of image links separated as commas and turning them into images but separating the list to show on different parts of the website.
My code
<?php
//the array, will be grabbing from database soon
$string = "main.jpg,extra_img_1.jpg,extra_img_2.jpg,extra_img_3.jpg,extra_img_4.jpg,extra_img_5.jpg";
//explode the whole array
$array = explode(',',$string);
//remaking the list turning it back into an array
$oldstring = '<li>'.implode('</li><li>',$array).'</li>';
// removing the first string in the commad array
array_shift($array);
//turning it back into an array without the first listed item
$newstring = '<li>'.implode('</li><li>',$array).'</li>';
//showing the old string in a listed format
echo $oldstring;
echo '<hr>';
// showing the first item in the old string
echo '<li>'.explode(',', $string)[0].'</li>';
echo '<hr>';
//once its shifted, i want to reshow the list
echo $newstring;
?>
Thanks for the upcoming advice.
I think your solution is pretty straightforward without much that is unnecessary. Note however, that array_shift() returns the shifted element. There is no need to explode()[0] it a second time:
// Linebreaks added to make the output readable
$string = "main.jpg,extra_img_1.jpg,extra_img_2.jpg,extra_img_3.jpg,extra_img_4.jpg,extra_img_5.jpg";
$array = explode(',',$string);
$oldstring = '<li>'.implode("</li>\n<li>",$array)."</li>\n";
$s0 = array_shift($array);
$newstring = '<li>'.implode("</li>\n<li>",$array)."</li>\n";
echo $oldstring;
echo "<hr>\n";
echo "<li>$s0</li>\n";
echo "<hr>\n";
echo $newstring;
Personally, I would probably just loop over the elements:
$string = "main.jpg,extra_img_1.jpg,extra_img_2.jpg,extra_img_3.jpg,extra_img_4.jpg,extra_img_5.jpg";
$array = explode(',',$string);
foreach ($array as $s)
echo "<li>$s</li>\n";
echo "<hr>\n";
$s0 = array_shift ($array);
echo "<li>$s0</li>\n";
echo "<hr>\n";
foreach ($array as $s)
echo "<li>$s</li>\n";
This question already has answers here:
Add quotation marks to comma delimited string in PHP
(5 answers)
Closed 1 year ago.
I have a variable with string value of 'laptop,Bag' and I want it to look like ' "laptop","Bag" 'or "laptop","Bag". How could I do this one? Is there any php function that could get this job done? Any help please.
This would work. It first, explodes the string into an array. And then implodes it with speech marks & finishes up by adding the opening & closing speech mark.
$string = "laptop,bag";
$explode = explode(",", $string);
$implode = '"'.implode('","', $explode).'"';
echo $implode;
Output:
"laptop","bag"
That's what str_replace is for:
$result = '"'.str_replace(',', '","', $str).'"';
This would be very easy to do.
$string = 'laptop,bag';
$items = explode(',', $string);
$newString = '"'.implode('","', $items).'"';
That should turn 'laptop,bag' into "laptop","bag".
Wrapping that in a function would be as simple as this:
function changeString($string) {
$items = explode(',', $string);
$newString = '"'.implode('","', $items).'"';
return $newString;
}
I think you can explode your string as array and loop throw it creating your new string
function create_string($string)
{
$string_array = explode(",", $string);
$new_string = '';
foreach($string_array as $str)
{
$new_string .= '"'.$str.'",';
}
$new_string = substr($new_string,-1);
return $new_string;
}
Now you simply pass your string the function
$string = 'laptop,Bag';
echo create_string($string);
//output "laptop","Bag"
For your specific example, this code would do the trick:
<?php
$string = 'laptop,bag';
$new_string = ' "' . str_replace(',', '","', $string) . '" ';
// $new_string: "laptop","bag"
?>
That code would also work if you had more items in that list, as long as they are comma-separated.
Use preg_replace():
$input_lines="laptop,bag";
echo preg_replace("/(\w+)/", '"$1"', $input_lines);
Output:
'"laptop","Bag"'
I think you can perform that using explode in php converting that string in to an array.
$tags = "laptop,bag";
$tagsArray = explode(",", $tags);
echo $tagsArray[0]; // laptop
echo $tagsArray[1]; // bag
Reference
http://us2.php.net/manual/en/function.explode.php
related post take a look maybe could solve your problem.
How can I split a comma delimited string into an array in PHP?
In a comma delimited string, in php, as such: "1,2,3,4,4,4,5" is it possible to say:
if(!/*4 is in string bla*/){
// add it via the .=
}else{
// do something
}
In arrays you can do in_array(); but this isn't a set of arrays and I don't want to have to convert it to an array ....
Try exploding it into an array before searching:
$str = "1,2,3,4,4,4,5";
$exploded = explode(",", $str);
if(in_array($number, $exploded)){
echo 'In array!';
}
You can also replace numbers and modify the array before "sticking it back together" with implode:
$strAgain = implode(",", $exploded);
You could do this with regex:
$re = '/(^|,)' + preg_quote($your_number) + '(,|$)/';
if(preg_match($re, $your_string)) {
// ...
}
But that's not exactly the clearest of code; someone else (or even yourself, months later) who had to maintain the code would probably not appreciate having something that's hard to follow. Having it actually be an array would be clearer and more maintainable:
$values = explode(',', $your_string);
if(in_array((str)$number, $values)) {
// ...
}
If you need to turn the array into a string again, you can always use implode():
$new_string = implode(',', $values);
I have a php value coming back from my database as a string, like
"this, that, another, another"
And I'm trying to wrap a separate link around each of those strings, but I can't seem to get it to work. I've tried a for loop, but since it's just a string of information and not an array of information that doesn't really work. Is there a way to wrap a unique link around each value in my string?
The easiest way that I see to do this would be using PHP's explode() function. You'll find that it will become very useful as you start to use PHP more and more, so do check out its documentation page. It allows you to split a string up into an array given a certain separator. In your case, this would be ,. So to split the string:
$string = 'this, that, another, another 2';
$parts = explode(', ', $string);
Then use a foreach (again, check the documentation) to iterate through each of the parts and make them into a link:
foreach($parts as $part) {
echo '' . $part . "\n";
}
However, you can do this with a for loop. Strings can be accessed like arrays, so you can implement a parser pattern to parse the string, extract the parts, and create the links.
// Initialize some vars that we'll need
$str = "this, that, another, another";
$output = ""; // final output
$buffer = ""; // buffer to hold current part
// Iterate over each character
for($i = 0; $i < strlen($str); $i++) {
// If the character is our separator
if($str[$i] === ',') {
// We've reached the end of this part, so add it to our output
$output .= '' . trim($buffer) . "\n";
// clear it so we can start storing the next part
$buffer = "";
// and skip to the next character
continue;
}
// Otherwise, add the character to the buffer for the current part
$buffer .= $str[$i];
}
echo $output;
(Codepad Demo)
A better way is to do it like this
$string = "this, that, another, another";
$ex_string = explode(",",$string);
foreach($ex_string AS $item)
{
echo "<a href='#'>".$item."</a><br />";
}
First explode the string to get the individual words in an array. Then add the hyperlinks to the words and finally implode them.
$string = "this, that, another, another";
$words = explode(",", $string);
$words[0] = $words[0]
$words[1] = $words[1]
..
$string = implode(",", $words);
You can also use the for loop to assign hyperlinks that follow a pattern like this:
for ($i=0; $i<count($words); $i++) {
//assign URL for each word as its name or index
}