I have the following string and need to cut it into pieces. But how to do this correct?
The string: $sting = 0nl10000143947019,23000143947456,....,10450143947022
... means that more nrs can be in here
Now I want to cut down this string in the following parts:
$first contains the first nr or letter in the string. So in this case it should be: 0
$second contains the second and third nrs or letters in the string. So in this case it should be: nl
$third contains EVERYTHING after the third nr or letter in the string. So in this case it should be: 10000143947019,23000143947456,....,10450143947022
$fourth contains EVERYTHING after the last , in the string. Note the this nr can be bigger or smaller than in this excample. So in this case it should be: 10450143947022
Hope this is possible!
Kind regards
With a simple regexp
([0-9A-Za-z]{1})([0-9A-Za-z]{2})(.*)\,([^\,]+)$
Should work. In PHP this would work like
$string = "0nl10000143947019,23000143947456,23000143947456,10450143947022";
preg_match("#([0-9A-Za-z]{1})([0-9A-Za-z]{2})(.*)\,([^\,]+)$#i", $string, $matches);
// map $matches to the right variables. But you can do that yourself :-)
Regex should do it, here's something to get you started
preg_match("/^(\d+)([a-z]+)(.*?,([^,]*))$/i", $string, $m);
print_r($m);
See here:
http://ideone.com/Xk1oV
Try the following:
$first = substr($sting,0,1);
$second = substr($sting,1,2);
$third = substr($sting,3);
$fourth = end(explode(',',$sting));
Related
I have a string that looks something like this:
abc-def-ghi-jkl-mno-pqr-stu-vwx-yz I'd like to get the content BEFORE the 4th dash, so effectively, I'd like to get abc-def-ghi-jkl assigned to a new string, then I'd like to get mno assigned to a different string.
How could I go about doing this? I tried using explode but that changed it to an array and I didn't want to do it that way.
Try this:
$n = 4; //nth dash
$str = 'abc-def-ghi-jkl-mno-pqr-stu-vwx-yz';
$pieces = explode('-', $str);
$part1 = implode('-', array_slice($pieces, 0, $n));
$part2 = $pieces[$n];
echo $part1; //abc-def-ghi-jkl
echo $part2; //mno
See demo
http://php.net/manual/en/function.array-slice.php
http://php.net/manual/en/function.explode.php
http://php.net/manual/en/function.implode.php
Can you add your source code? I done this one before but I cant remember the exact source code I used. But I am pretty sure I used explode and you can't avoid using array.
EDIT: Mark M answer is right.
you could try using substr as another possible solution
http://php.net/manual/en/function.substr.php
If I see where you are trying to get with this you could also go onto substr_replace
I guess an alternative to explode would be to find the position of the 4th - in the string and then get a substring from the start of the string up to that character.
You can find the position using a loop with the method explained at find the second occurrence of a char in a string php and then use substr(string,0,pos) to get the substring.
$string = "abc-def-ghi-jkl-mno-pqr-stu-vwx-yz";
$pos = -1;
for($i=0;$i<4;$i++)
$pos = strpos($string, '-', $pos+1);
echo substr($string, 0, $pos);
Code isn't tested but the process is easy to understand. You start at the first character (0), find a - and on the next loop you start at that position +1. The loop repeats it for a set number of times and then you get the substring from the start to that last - you found.
I was once asked a question in an interview that if we have 2 strings, how we can get a part of matching string from these two?
for example
$str1 = "My name is baig";
$str2 = "Baig is a nice person";
now i want the output
"i a e s g" and other matching letters
I want this in Php
Thanks in advance
this is code to get the expected result for two string str1 and str2
<?php
$str1 = strtolower("My name is baig");
$str2 = strtolower("Baig is a nice person");
$array1 = explode(' ',$str1);
$array2 = explode(' ',$str2);
$result = array_intersect($array2, $array1);
print_r(implode(' ',$result));
?>
Well you have several options...
If you want matches in a specific order, meaning is a string inside another string, you might go for strstr, if you want to find any common point between 2 strings, you might want to first find an explode pattern, in your case a space.
Expode both string and intersect both arrays.
You can use array_intersect after use the function explode on your string.
A working example :
http://codepad.org/IikNr2Vv
But this is case sensitive, Baig will be different of baig.
To resolve this, you can use the strtolower function to transform to lower cas all your string.
got the following answer from another forum
<?php
$str1 = "My name is baig";
$str2 = "Baig is a nice person";
$r1 = str_split(preg_replace("/[^a-zA-z]/","",strtolower($str1)));
$r2 = str_split(preg_replace("/[^a-zA-z]/","",strtolower($str2)));
$match = array_unique(array_intersect($r1,$r2));
$match = implode(" ",$match);
echo "Matched letters are: ".$match;
?>
Case sensitivity is still an issue but i will go through it
When I've a string:
$string = 'word1="abc.3" word2="xyz.3"';
How can I replace the point with a comma after xyz in xyz.3 and keep him after abc in abc.3?
You've provided an example but not a description of when the content should be modified and when it should be kept the same. The solution might be simply:
str_replace("xyz.", "xyz", $input);
But if you explicitly want a more explicit match, say requiring a digit after the ful stop, then:
preg_replace("/xyz\.([0-9])+/", 'xyz\${1}', $input);
(not tested)
something like (sorry i did this with javascript and didn't see the PHP tag).
var stringWithPoint = 'word1="abc.3" word2="xyz.3"';
var nopoint = stringWithPoint.replace('xyz.3', 'xyz3');
in php
$str = 'word1="abc.3" word2="xyz.3"';
echo str_replace('xyz.3', 'xyz3', $str);
You can use PHP's string functions to remove the point (.).
str_replace(".", "", $word2);
It depends what are the criteria for replace or not.
You could split string into parts (use explode or preg_split), then replace dot in some parts (eg. str_replace), next join them together (implode).
how about:
$string = 'word1="abc.3" word2="xyz.3"';
echo preg_replace('/\.([^.]+)$/', ',$1', $string);
output:
word1="abc.3" word2="xyz,3"
I have this string:
a:3:{i:0;i:2;i:1;i:3;i:2;i:4;}
I want to get number between "a:" and ":{" that is "3".
I try to user substr and strpos but no success.
I'm newbie in regex , write this :
preg_match('/a:(.+?):{/', $v);
But its return me 1.
Thanks for any tips.
preg_match returns the number of matches, in your case 1 match.
To get the matches themselves, use the third parameter:
$matches = array();
preg_match(/'a:(\d+?):{/', $v, $matches);
That said, I think the string looks like a serialized array which you could deserialize with unserialize and then use count on the actual array (i.e. $a = count(unserialize($v));). Be careful with userprovided serialized strings though …
If you know that a: is always at the beginning of the string, the easiest way is:
$array = explode( ':', $string, 3 );
$number = $array[1];
You can use sscanfDocs to obtain the number from the string:
# Input:
$str = 'a:3:{i:0;i:2;i:1;i:3;i:2;i:4;}';
# Code:
sscanf($str, 'a:%d:', $number);
# Output:
echo $number; # 3
This is often more simple than using preg_match when you'd like to obtain a specific value from a string that follows a pattern.
preg_match() returns the number of times it finds a match, that's why. you need to add a third param. $matches in which it will store the matches.
You were not too far away with strpos() and substr()
$pos_start = strpos($str,'a:')+2;
$pos_end = strpos($str,':{')-2;
$result = substr($str,$pos_start,$pos_end);
preg_match only checks for appearance, it doesn't return any string.
I have some testcases/strings in this format:
o201_01_01a_Testing_to_see_If_this_testcases_passes:without_data
o201_01_01b_Testing_to_see_If_this_testcases_passes:data
rx01_01_03d_Testing_the_reconfiguration/Retest:
Actually this testcase name consists of the actual name and the description.
So, I want to split them like this :
o201_01_01a Testing_to_see_If_this_testcases_passes:without_data
o201_01_01b Testing_to_see_If_this_testcases_passes:data
rx01_01_03d Testing_the_reconfiguration/Retest:
I am unable to figure out the exact way to do this in explode in php
Can anyone help please?
Thanks.
If the first part has always the same length, why don't you use substr, e.g.
$string = "o201_01_01a_Testing_to_see_If_this_testcases_passes:without_data";
$first_part = substr($string, 0, 11); // o201_01_01a
$second_part = substr($string, 12); // Testing_to_see_If_this_testcases_passes:without_data
$results = preg_split("/([a-z0-9]+_[0-9]+_[0-9]+[a-z])(.*)/", $input);
That should give you an array of results, provided I got the regular expression correct.
http://www.php.net/manual/en/function.preg-split.php
Looking at the pattern, it appears that you need to use regular expressions. If this is how they all are, you can cut off the beginning by looking for an upper case character. The code might look like this:
$matches = array()
preg_match('/^[^A-Z]*?/', $string, $matches);
$matches = substr($matches[0], 0, count($matches[0])-1);
Would put the first little part into $matches. Working on second part...