How can I remove numbers from a string like "take_me_apart_12_13" to make it "take_me_apart" and save the numbers (12 & 13) into an array?
I've tried things like preg_split("/_/", $string) but that breaks the whole structure of the string (instead of just removing the numbers and keeping the words and underscores. I've been looking for a php function to use for this sort of thing but I cannot find one that accomplishes this task.
I know I could do something along the lines of using a for loop and checking each character and creating a new string in there but I was hoping to avoid that. If it's the only possible solution though, I'll have to use it.
use this [0-9] with _
$data = "take_me_apart_12_13";
preg_match_all('!\d+!', $data , $matches);
print_r($matches);
echo $words = preg_replace('/_[0-9]+/', '', $data);
DEMO
OUTPUT :: take_me_apart
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)
I am trying to split a string with preg_split, take the results, surround them with custom data, and reinsert them into the original string.
For example:
$string="this is text [shortcode] this is also text [shortcode2] this is text";
Afterwards, I want string to equal:
$string="<span id='0'>this is text </span>[shortcode]<span id='1'> this is also text </span>[shortcode2]<span id='2'>this is text</span>";
I was successful at splitting the string into an array:
$array=preg_split(/\[[^\]]*\]/,$string);
I then tried a for next loop to replace the values - which worked OK, except if the array values had identical matches in the string.
Any help is appreciated - or another approach that might be better.
preg_split() is the wrong tool for this, I would suggest using a callback.
Below is an example to get you started in which you can modify it to your needs along the way.
$str = preg_replace_callback('~(?:\[[^]]*])?\K[^[\]]+~',
function($m) {
static $id = 0;
return "<span id='".$id++."'>$m[0]</span>";
}, $str);
eval.in demo
I have an alphanumeric string like 1234and5678.
I want to store the numbers preceding and i.e 1234 into one variable and the number after and i.e 5678 into another variable as shown below:
$var1=1234;
$var2=5678;
also what should do if i replace and by some random special characters like #$% etc.
Can you please help me out?
Thanks in advance.
$string = "1234and5678";
list($before, $after) = explode("and", $string);
This splits the string into two variables based on the delimeter ("and"), whatever is before and is saved in a variable called $before, whatever is after is saved into a variable called $after
Use split http://php.net/manual/en/function.split.php with "and" as separator. That should give you an array with the two numbers.
you can use preg_split() function, in your case: $var = preg_split("/[\D]+/", "1234and5678"); That gives you $var[0] = '1234' and $var[1] = '5678'
According to you edit, you can:
replace regex on any another that you need (e.g. $var = preg_split("/[\d]+/", "1234and5678"); for any non-numeric element)
use preg_match_all("/(\d+)/","1234and5678", $var) to find any numbers in your string
Use regular expressions. I provided a link for regexes in php. Split is deprecated as of version 5.3.0 and you should not rely on it.
Perl compatible regular expressions
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.