how to keep a specific string in php without split - php

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.

Related

PHP- Parsing words from a string without spaces?

My webpage has a variable, $currentPage. This is a string of the php token name of the page I'm currently on.
Example: All categories under the user section have names such as:
uAdminNew, uAdminEdit, ect..
I would like for a way to parse out the uAdmin and just determine what is the last word (New and Edit) and call upon functions from there.
I have my navigation system working through these names, therefore I can't change the names or I would to make it easier to parse. Such as adding delimiters.
Is this something only Regex can solve or is there a simpler solution I'm missing? If this is Regex could you explain or provide a link as to how I would go about using it to test against a specific list of strings? I'm very new to it.
For example, so:
$str = 'uAdminEdit';
$ar = preg_match('/([A-Z][^A-Z]+$)/', $str, $m);
echo $m[1]; // Edit
Does the pagename always start with uAdmin? If so, you could split the string by "uAdmin" with explode():
$page = 'uAdminEdit';
echo explode('uAdmin', $page)[1]; //Output: Edit
Or simply remove "uAdmin" with str_replace():
$page = 'uAdminEdit';
echo str_replace('uAdmin', '', $page); //Output: Edit
If you just want the section after uAdmin, use the regex capture groups
preg_match('/uAdmin(.*)/', $sub, $matches);
echo $matches[1]

Pass a string into an array PHP

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)

How to extract substring between two substrings from a string in php?

For example:
$string="<aa>xyz<bb>123<ccc>";
I want to get the substring 'xyz' from $string.Is it possible?
Simply you can use strip_tags to get xyz
<?php
echo strip_tags('<aa>xyz<bb>');
?>
strip_tags is only striping tags. Use it only if this meets your requirement.
You can also use a regex to explode the string into an array. this would give you the substring you want in $arr[0]
This will work regardless what the tags are, and allow you to easily which ever substring you want.
<?php
$string="<aa>xyz<bb>123<ccc>";
$arr = preg_split("/<[^>]+>/", $string, 0, PREG_SPLIT_NO_EMPTY);
var_export($arr);
yes it is possible
$string="<aa>xyz<bb>";
echo substr($string,4,4);
you can also do this by strip_tags() it Strip HTML and PHP tags from a string
Use preg_match with <aa>(.*?)</bb> , it will be the first match.
With regular expressions you will also be able to get other matches in your string also ..

Splitting a string and adding to array

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.

preg_replace on the matches of another preg_replace

I have a feeling that I might be missing something very basic. Anyways heres the scenario:
I'm using preg_replace to convert ===inputA===inputB=== to inputA
This is what I'm using
$new = preg_replace('/===(.*?)===(.*?)===/', '$1', $old);
Its working fine alright, but I also need to further restrict inputB so its like this
preg_replace('/[^\w]/', '', every Link or inputB);
So basically, in the first code, where you see $2 over there I need to perform operations on that $2 so that it only contains \w as you can see in the second code. So the final result should be like this:
Convert ===The link===link's page=== to The link
I have no idea how to do this, what should I do?
Although there already is an accepted answer: this is what the /e modifier or preg_replace_callback() are for:
echo preg_replace(
'/===(.*?)===(.*?)===/e',
'"$1"',
'===inputA===in^^putB===');
//Output: inputA
Or:
function _my_url_func($vars){
return ''.$vars[2].'';
}
echo preg_replace_callback(
'/===(.*?)===(.*?)===/',
'_my_url_func',
'===inputA===inputB===');
//Output: inputB
Try preg_match on the first one to get the 2 matches into variables, and then use preg_replace() on the one you want further checks on?
Why don't you do extract the matches from the first regex (preg_match) and treat thoses results and then put them back in a HTML form ?

Categories