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
Related
I got now a two sides that contains numbers and between two specific numbers there is a string that shows a group of numbers, Let's say we got this 123456789$numbers1234567 and I want to get the result of $numbers so how can I get it?
Thanks
If you know the two strings that it is sandwiched between then you can strip out the strings that you are looking for.
Not too elegant but this works:
$str1 = "123456789";
$str2 = "1234567";
$numberstr = "123456789";
$searchstring = "123456789".$numberstr."1234567";
$limit = 1;
$numbers = substr($searchstring, 0, strlen($searchstring) - strlen($str2)); // Remove the end of the string with length = $str2
$numbers = substr($numbers, strlen($numbers) - strlen($str1)); // Remove the most string from the beginning
print $numbers;
Output:
123456789
In summary, it removes the known string from the end, then the other known string from the beginning.
UPDATE: as per the comments, use two substrs to find the wanted string
I need to slice a string into 2 parts, such that, for example, if the string is "4s5ee9f8fg", I need it as "4598 seeffg"
I'm trying with this:
$string2 = '132xx';
preg_match("/[0-9]+/",trim($string2),$result);
echo $result[0];
echo $result[1];
Here I'm getting just numeric characters, but not alphabetic characters.
Can anyone give a solution?
You're on the right track with what you have.
You are currently only trying to get the numeric characters, which is why that's all you are getting. Also, after some testing with different strings, it turned out that your particular regex seems to only works when all the numbers are next to each other. I figured out a way to do this with preg_replace instead of preg_match.
Try this:
$string2 = '4s5ee9f8fg';
$result1 = preg_replace("/[A-z]+/", "", trim($string2));
$result2 = preg_replace("/[0-9]+/", "", trim($string2));
$finalResult = $result1." ".$result2;
echo $finalResult."\n";
i have a specific problem .I am retrieving data in string format in PHP. I have to seperate the specific values from the string.My string looks like this
Barcode formatQR_CODEParsed Result TypeURIParsed Resulthttp://www.myurl.co.uk var ga.
Barcode formatQR_CODEParsed Result TypeTEXTParsed ResultMy Coat var ga.
As you can see from above two examples, The text after "Barcode formatQR_CODEParsed Result Type" and "Parsed Result" are changing.
I have tried strstr function but it is not giving me desired output as the words "Parsed Result" is repeating twice.How can i extract ANY value /text that will come after these?How can I separate them?I would appreciate if someone could guide as i am new bee.
Thanks
The fastest way is to parse this piece of HTML code with SimpleXML and get <b> children's values.
just walk through the strings until you find the first difference. Shouldn't be a problem?
$str1 = "Hello World";
$str2 = "Hello Earth";
for($i=0; $<min(strlen($str1),strlen($str2)); $i++){
if ($str1[$i] != $str2[$i]){
echo "Difference starting at pos $i";
}
}
or something like that. Then you can use substr to remove the equal part.
edit: IF your Strings always have the same Pattern, with values enclosed in <b> you can perfectly use a Regular-Expression to get the values.
I have found the solution .We can extract strings this way:
<?
$mycode='Barcode formatQR_CODEParsed Result TypeURIParsed Resulthttp://www.myurl.co.uk var ga';
$needle = 'Parsed Result';
$chunk=explode($needle,$mycode);
$mychunky= $chunk[2];
$needle = 'var ga';
$result = substr($mychunky, 0, strpos($mychunky, $needle));
print($result);
?>
This should work for you. Also you can extend this idea futher and develop by your own needs.
$string = "Barcode formatQR_CODEParsed Result TypeURIParsed Resulthttp://www.myurl.co.uk var ga." ;
$matches = array() ;
$pattern = "/Type([A-Z]+)Parsed Result([^>]+) var ga./" ;
preg_match($pattern, $string, $matches) ; // Returns boolean, but we need matches.
Then get the occurences:
$matches[0] ; // The whole occurence
$matches[1] ; // Type - "([A-Z]+)"
$matches[2] ; // Result - "([^>]+)"
So you use elements with indeces 1 and 2 as Type and Result respectively. Hope it can help.
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));
I have strings like:
t_est1_1
test213_4
tes_tsdfsdf_9
The common part of every string is the LAST underscore _ character.
I need to get the string before this character.
t_est1_12 --> test1
test213_4 --> test213
tes_tsdfsdf_9343 --> testsdfsdf
How can i achieve this in PHP?
Using the basic string functions strpos and substr.
http://fr.php.net/manual/fr/function.explode.php
$a = "abcdef_12345"
$b = array();
// $b[0] = "abcdef";
$b[0] = explode('_',$a,'1');
you can use preg_match function available in php
you need to write regular expression for that...
for example
to get this test1_12 ->> test1
$string='test1_12';
preg_match('((.+?)\_(.*))',$string,$match);
echo $match[1];
What you want is a simple explode, array_slice and implode, also using explode and end, you can get the "id" that is the common part too:
$description = implode('', array_slice(explode('_', $data), 0, -1));
$id = end(explode('_', $data));
As many _ you will have, you'll still be able to expode on them and retrieve the last item containing your id and the first items (0 to -1) will contain your description...