I have a string
$str = "a,b,c,d,e";
And I want convert string as:
$str_convert = "'a','b','c','d','e'";
What should I do?
Try my solution:
<?php
$str = "a,b,c,d,e";
$arr = explode(',',$str);
foreach ($arr as &$value) {
$value = "'$value'";
}
$str_convert= implode(',', $arr);
echo $str_convert;
Like this:
$str = "a,b,c,d,e";
$items = split(",", $str);
$convert_str = "";
foreach ($items as $item) {
$convert_str .= "'$item',";
}
$convert_str = rtrim($convert_str, ",");
print($convert_str);
If you would like a different solution using functional programming coding style, here it is:
<?php
$str = 'a,b,c,d,e';
$add_quotes = function($str, $func) {
return implode(',', array_map($func, explode(',', $str)));
};
print $add_quotes(
$str,
function ($a) {
return "'$a'";
}
);
Related
I am trying to merge two strings in a specific way.
Essentially I need to merge the first two strings into a third with a pipe symbol between them then separated by commas:
$merge1 = "id1,id2,id3"
$merge2 = "data1,data2,data3"
Those two would become:
$merged = "id1|data1,id2|data2,id3|data3"
I hope this makes sense?
I mean there is no PHP function that can output what you need.
Code produced desired output could be
<?php
$merge1 = "id1,id2,id3";
$merge2 = "data1,data2,data3";
$merged = [];
$arr1 = explode(',', $merge1);
$arr2 = explode(',', $merge2);
foreach ($arr1 as $key => $val) {
$merged[] = $val . '|' . $arr2[$key];
}
echo implode(',', $merged);
// id1|data1,id2|data2,id3|data3
This script will help you
<?php
$merge1 = "id1,id2,id3";
$merge2 = "data1,data2,data3";
$merge1 = explode(",", $merge1);
$merge2 = explode(",", $merge2);
$final = [];
foreach ($merge1 as $index => $value) {
$final[] = $value . "|" . $merge2[$index];
}
$final = implode(",", $final);
print_r($final);
output
id1|data1,id2|data2,id3|data3
Try this.
<?php
$merge1 = "id1,id2,id3";
$merge2 = "data1,data2,data3";
$merge1 = explode(",",$merge1);
$merge2 = explode(",",$merge2);
$mergeArr = array_combine($merge1,$merge2);
$mergeStr = [];
foreach($mergeArr as $k => $v) {
$mergeStr[] = $k.'|'.$v;
}
$mergeStr = implode(",",$mergeStr);
echo $mergeStr;
?>
I have string and I want to splitt it to array and remove a specific part and then convert it back to a string .
$string = "width,100;height,8600;block,700;narrow,1;"
I want to search block in this string and remove it with its value "block,700;" and get the string as "width,100;height,8600;narrow,1;"
below is my code php which i tried.Please advice me
$outerARR = explode(";", $string);
$arr = array();
foreach ($outerARR as $arrvalue) {
$innerarr = explode(",", $arrvalue);
$arr[] = $innerarr;
}
for ($i = 0; $i < count($arr); $i++) {
if (in_array($arr, 'block')) {
unset($arr[$i]);
}
}
Please note that "block" in aboive string will not always contain and the value may differ. So I can't use string replace . please advice
You essentially want to replace part of your string:
$string = "width,100;height,8600;block,700;narrow,1;";
$regex = '#(block,(.*?);)#';
$result = preg_replace($regex, '', $string);
echo $result;
Try this:
$string = "width,100;height,8600;block,700;narrow,1;"
$outerARR = explode(";", $string);
$arr = array();
foreach ($outerARR as $arrvalue) {
$innerarr = explode(",", $arrvalue);
$arr[$innerarr[0]] = $innerarr[1];
}
if (array_key_exists('block', $arr)) {
unset($arr['block']);
}
I'm having trouble using preg_match_all to split a string into key value pairs. An example of my string:
"%Title:Movie%Sortable%Writer:%Indexed:false%"
Where I expect results like:
$result['Title'] = 'Movie';
$result['Sortable'] = '';
$result['Writer'] = '';
$result['Indexed'] = 'false';
I can split the string using preg_match('/%/',$str,-1,PREG_SPLIT_NO_EMPTY); but it returns an indexed array. I need an associative array so that order is not important and I can use the key in a switch statement. What would be the correct regex to use in preg_match_all?
Try with:
$input = "%Title:Movie%Sortable%Writer:%Indexed:false%";
$output = array();
$data = explode('%', $input);
foreach ($data as $item) {
list($key, $value) = explode(':', $item);
$output[$key] = $value;
}
<?php
$arr = array();
$string = "%Title:Movie%Sortable%Writer:%Indexed:false%";
$d = explode('%', $string);
foreach($d as $item){
list($key,$value) = explode(':', $item);
$arr[$key] = $value;
}
print_r($arr);
?>
I got this
$description = '<p>text1</p><p>text2</p><p>text3</p><p>text4</p><p>textn</p>'
I want to remove only what comes after <p>text3</p>
My result would be:
$description = '<p>text1</p><p>text2</p><p>text3</p>'
My guess is that we need to use preg_replace with some regex but I can't manage to write a working one.
You could...
function str_occurance($needle, $haystack, $occurance) {
$occurance += 2;
$arr = explode($needle, $haystack, $occurance);
unset($arr[0]);
$arr = array_values($arr);
$key = count($arr) - 1;
unset($arr[$key]);
$str = $needle . implode($needle, $arr);
return $str;
}
Not the prettiest, but it works.
Edit: To use:
$description = '<p>text1</p><p>text2</p><p>text3</p><p>text4</p><p>textn</p>';
$split = '<p>';
$return = 3;
$new = str_occurance($needle, $description, $return);
echo $new; // returns <p>text1</p><p>text2</p><p>text3</p>
In the following code, is it possible to spilt the Object $author where white space occurs ?
<?php
$url="http://search.twitter.com/search.rss?q=laugh";
$twitter_xml = simplexml_load_file($url);
foreach ($twitter_xml->channel->item as $key) {
$a = $key->{"author"};
echo $a;
}
?>
$split = explode(' ', (string) $key->{"author"}));
OR
$split = preg_split('/\s+/', (string) $key->{"author"}));
To split by # just take $split and run in loop
foreach($split as $key => $value) {
$eta = explode('#', $value);
var_dump($eta);
}
To check if string exist use strpos
foreach($split as $key => $value) {
if (strpos($value, '#') !== 0) echo 'found';
}
Use explode:
$array = explode(' ', $key->{"author"});
There is an explode function that can easily accomplish that. So, for example:
$a = $key->{"author"};
$author = explode(" ", $a);
$first_name = $author[0];
$last_name = $author[1];
Hope that helps.
Assuming you merely care to get 2 parts: email, and "friendly name" (cause people have 1 to n number of names).
<?php
$url="http://search.twitter.com/search.rss?q=laugh";
$twitter_xml = simplexml_load_file($url);
foreach ($twitter_xml->channel->item as $key) {
$a = $key->{"author"};
preg_match("/([^ ]*) (.*)/", $a, $matches);
print_r($matches);
echo "\n";
}
?>