I have list of strings like this:
'PYRAMID','htc_europe','htc_pyramid','pyramid','pyramid','HTC','1.11.401.110
CL68035
release-keys','htc_europe/pyramid/pyramid:4.0.3/IML74K/68035.110:user/release-keys'
It looks like elements of an array,
But when i use
<?php
$string = "'PYRAMID','htc_europe','htc_pyramid','pyramid','pyramid','HTC','1.11.401.110 CL68035 release-keys','htc_europe/pyramid/pyramid:4.0.3/IML74K/68035.110:user/release-keys'";
$arr = array($string);
print_r($arr);
?>
It doesnt work as I want:
Array ( [0] =>
'PYRAMID','htc_europe','htc_pyramid','pyramid','pyramid','HTC','1.11.401.110
CL68035
release-keys','htc_europe/pyramid/pyramid:4.0.3/IML74K/68035.110:user/release-keys')
Instead of:
Array ( [0] => PYRAMID, [1] => htc_europe, [2] => htc_pyramid,
...
I dont want to use explode() because my strings are already in array format and many strings have the ',' character.
Please help me, thanks.
Your string is not in an array format. From the way it looks and based on your comments, I would say that you have comma separated values, CSV. So the best way to parse that would be to use functions specifically made for that format like str_getcsv():
$str = "'PYRAMID','htc_europe','htc_pyramid','pyramid','pyramid','HTC','1.11.401.110 CL68035 release-keys','htc_europe/pyramid/pyramid:4.0.3/IML74K/68035.110:user/release-keys'";
// this will get you the result you are looking for
$arr = str_getcsv($str, ',', "'");
var_dump($arr);
The use of the second and third parameters ensures that it gets parsed correctly also when a string contains a comma.
$string is still a string, so you explode it if you want to make an array out of it.
If your problem is strings have the ',' character, use some other seperator, maybe |
$string = "'PYRAMID'|'htc_europe'|'htc_pyramid'|'pyramid'|'pyramid'|'HTC'|'1.11.401.110 CL68035 release-keys'|'htc_europe/pyramid/pyramid:4.0.3/IML74K/68035.110:user/release-keys'";
$arr = explode('|',$string);
print_r($arr);
<?php
$int = preg_match_all(
"/'(.+?)'/",
"'PYRAMID','htc_europe','htc_pyramid','pyramid','pyramid','HTC','1.11.401.110 CL68035 release-keys','htc_europe/pyramid/pyramid:4.0.3/IML74K/68035.110:user/release-keys'",
$matches);
print_r($matches[1]);
You can test it here http://micmap.org/php-by-example/en/function/preg_match_all
Due to the edits in the question, my answer is now out of date. I will leave it here because it contains a little explanation why in a particular case explode will be a valid solution.
as you can read in the manual online of php, there is a very precise syntax that can be used when creating an array, this is the reference:
http://php.net/manual/en/function.array.php
As you can see the correct way to use array() to create a new array is declaring each value separated by a comma or by declaring each pair index => value separated by a comma.
There is -no way- to pass a single string to that method (I see it something json like in javascript or java maybe, but this is Off Topic) simply because it won't parse it, the method will take the whole string as is and of course putting it into a single index (that in your case will be index zero).
I am telling you of course to use explode() or split() or to parse your string before, and what I told you before is the reason to my statement.
You probabily want to have each single model of phone in a string inside the array so you will have to remove the single quote first:
$stringReplaced = str_replace("'", "", $yourString);
And then you will have to split the string into an array using:
$array = explode(',',$yourString);
I hope you will take this in consideration
Of course as told by my collegue up there, you can treat this string as a comma separated value and use str_getcsv.
~Though you will need to remove the single quotes to have the pure string.~
(last statement is wrong because you can use the enclosure char param provided by str_getcsv)
Related
I am creating a PHP template page that I dont want the end-user to have to write PHP code in. So I would like to set up a system that replaces [[string]] with $string. This way I dont have to maintain a list of variables to look up, I can just use the name within the brackets and if there is a matching variable in the code, it will replace it.
I know how to capture all the double bracketed text.
$text = 'string [[of]] text [[to]] test'
preg_match_all("/\[\[[^\]]*\]\]/", $text, $matches);
I think that preg_match_all returns an array of all the matches, but the only way I can seem to have access into it is something like $matches[1], but this won't work since I dont know how many matches there will be.
I have tried
foreach ($matches as $match) {
///code
}
But I think this foreach is looking at the whole array, not the inside.
Once I get each match I know how to remove the two brackets and turn it into a variable PHP will recognize, instead of a string.
str_replace($match, ${substr($match, 2, -2)}, $text)
If you have a better idea of how to turn double bracketed text in HTML file into variables PHP will read without knowing what is in the double brackets first I would be interested in another solution as well.
preg_match_all('/\[\[.*?\]\]/',$string,$matches);
Will give $matches:
Array
(
[0] => Array
(
[0] => [[of]]
[1] => [[to]]
)
)
So you can loop $matches[0]
I have a string like this : xxxxxxx=921919291&arg3=3729ABNTSC980z2MNM3573&arg2=2025102&arg1=7e266505e183fcb31d0ba493008fa9f881af6746.
i want to keep only the xxxxxxx=921919291 (this one is variable so i can't use an strpos)
I have tried an explode of & caractere then show only the 0 one but i have a lot of string in the same variable so it would not but good.
STR_REPLACE dosn't seems to be good because all caracteres after the = caractere are variable.
use parse_str
$str = "xxxxxxx=921919291&arg3=3729ABNTSC980z2MNM3573&arg2=2025102&arg1=7e266505e183fcb31d0ba493008fa9f881af6746";
parse_str($str);
echo $xxxxxxx;
You can also place your values into an array like so:
$str = "xxxxxxx=921919291&arg3=3729ABNTSC980z2MNM3573&arg2=2025102&arg1=7e266505e183fcb31d0ba493008fa9f881af6746";
parse_str($str, $output);
echo $output['xxxxxxx'];
More information can be found here: http://php.net/manual/en/function.parse-str.php
Try this:
echo array_shift(explode('&', $string));
You should look into phps capturing groups, an expression like this, should sort you:
^([^&]+)
This will populate capture group 1, which is accessed using the parameter of the preg_match call as an array element.
I am trying to replace few words from a huge string with preg_replace()
using it like this :
preg_replace($match[0], $variable_value_array, $form_body)
Here $match[0] is an array with value in it in the form like:
$contacts-firstname$
$contacts-lastname$
$contacts-mobile$
$leads-leadstatus$
$leads-noofemployees$
and $variable_value_array is also an array with values in it like :
Linda
William
(091) 115-9385
Value Not Present
Value Not Present
and $form_body is a really long string.
The function is replacing the values of $form_body but instead of replacing the whole $contacts-firstname$ with Linda it is replacing only contacts-firstname with Linda making it like $Linda$. What should i do to replace both the $ sigh's as well ?
Thanks.
That's because $ is interpreted as the delimiter of your regex. You should use a simple str_replace instead. The signature is exactly the same:
str_replace($match[0], $variable_value_array, $form_body);
If you desperately wanted to use preg_replace you need to do two things. Firstly, you need to wrap every array element in explicit delimiters (/ is kind of the standard choice). And also you need to run every array element through preg_quote, otherwise the $ will be treated as an end-of-string anchors:
$patterns = array();
foreach($match[0] as $value)
$patterns[] = '/'.preg_quote($value, '/').'/';
And then use $patterns instead of $match[0]. But that is only intended as a nice-to-know if you ever actually need to use an array of literal search-strings inside a more complex pattern.
Try str_replace instead of preg_replace its faster and much appropriate for your use case
I have some data that is posted from a java application to a php page. The data is posted in the form of a string of numbers, each seperated by a comma - e.g. "1,2,3,4,5,6,7,8". The string could have different amounts of numbers there is nothing definite. I know how to split the string up but how would I go about adding it to an array in PHP? I'm completely new to PHP!
You could use the function explode(). Have a look at the manual.
For example:
$str = "1,2,3,4,5,6,7,8";
$arr = explode(',', $str);
just use the function explode
look at http://php.net/explode
If you "know how to split the string up", then you already know that this process produces an array.
I'm receiving a query string (from a terrible payment system whose name I do not wish to sully publicly) that contains un-encoded ampersands
name=joe+jones&company=abercrombie&fitch&other=no
parse_str can't handle this, and I don't know enough of regex to come up with my own scheme (though I did try). My hang up was look-ahead regex which I did not quite understand.
What I'm looking for:
Array
(
[name] => joe jones
[company] => abercrombie&fitch
[other] => no
)
I thought about traipsing through the string, ampersand by ampersand, but that just seemed silly. Help?
How about this:
If two ampersands occur with no = between them, encode the first one. Then pass the result to the normal query string parser.
That should accomplish your task. This works because the pattern for a "normal" query string should always alternate equals signs and ampersands; thus two ampersands in a row means one of them should have been encoded, and as long as keys don't have ampersands in them, the last ampersand in a row is always the "real" ampersand preceding a new key.
You should be able to use the following regex to do the encoding:
$better_qs = preg_replace("/&(?=[^=]*&)/", "%26", $bad_qs);
You could also use the split() function to split the string by ampersands. After that, you could split again each element with the delimeter "="... something like that:
$myarray = split("&", $mystring);
foreach ($myarray as $element) {
$keyvalue = split("=", $element);
$resultarray[$keyvalue[0]] = $keyvalue[1];
}
print_r($resultarray);
Not tested! But you should get the idea.