PHP get a part of string - php

How can I check in PHP whether a string contains '-'?
Example
ABC-cde::abcdef
if '-' is found
then I have to perform split() to split ABC from cde::abcdef
else
no need to perform split()
like cde::abcdef

if (strpos($string, "-") !== false)
{
split();
}

Just use explode that should be sufficient
eg. explode ('-',$urstring);
This will only split it (into an array of strings) if "-" exist else return the entire string as a array

How about just using the $limit parameter of explode()?
This will return an array in both your examples, with only one element in the latter case.
Note that split() is deprecated as of PHP 5.3: http://php.net/manual/en/function.split.php
$s1 = 'ABC-cde::abcdef';
$s2 = 'cde::abcdef';
$s3 = 'ABC-with-more-hyphens';
explode('-', $s1, 2);
// array(2) {
// [0]=>
// string(3) "ABC"
// [1]=>
// string(11) "cde::abcdef"
// }
explode('-', $s2, 2);
// array(1) {
// [0]=>
// string(11) "cde::abcdef"
// }
explode('-', $s3, 2);
// array(2) {
// [0]=>
// string(3) "ABC"
// [1]=>
// string(17) "with-more-hyphens"
// }

Just split() it and count the elements in the return array. Maybe it's even enough to continue with the first (or last) element, e.g. $newstring = split($oldstring, '-')[0]...

Related

php function for rading out values from string

i want to make a php loop that puts the values from a string in 2 different variables.
I am a beginner. the numbers are always the same like "3":"6" but the length and the amount of numbers (always even). it can also be "23":"673",4:6.
You can strip characters other than numbers and delimiters, and then do explode to get an array of values.
$string = '"23":"673",4:6';
$string = preg_replace('/[^\d\:\,]/', '', $string);
$pairs = explode(',', $string);
$pairs_array = [];
foreach ($pairs as $pair) {
$pairs_array[] = explode(':', $pair);
}
var_dump($pairs_array);
This gives you:
array(2) {
[0]=>
array(2) {
[0]=>
string(2) "23"
[1]=>
string(3) "673"
}
[1]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "6"
}
}
<?php
$string = '"23":"673",4:6';
//Remove quotes from string
$string = str_replace('"','',$string);
//Split sring via comma (,)
$splited_number_list = explode(',',$string);
//Loop first list
foreach($splited_number_list as $numbers){
//Split numbers via colon
$splited_numbers = explode(':',$numbers);
//Numbers in to variable
$number1 = $splited_numbers[0];
$number2 = $splited_numbers[1];
echo $number1." - ".$number2 . "<br>";
}
?>

PHP extract number only from result explode

I have explode here is the result of var_dump:
and I want get the result from explode:
7 [remove other string]
0 [if contain "-"]
0 [if contain "-"]
0 [if contain "-"]
Here, I just used comma as delimiter:
var_dump (explode(",", $rowData[0][13]));
die();
Does anyone have the solution to solved this?
Thank You.
Use array_map() function and in it use filter_var() to sanitize number value.
Try
$rowData = ['JJ7', 'B-', 'S-', 'M-'];
$result = array_map(function($v) {
return abs((int) filter_var($v, FILTER_SANITIZE_NUMBER_INT));
}, $rowData );
var_dump( $result );
//output
// array(4) { [0]=> int(7) [1]=> int(0) [2]=> int(0) [3]=> int(0) }
<?php
$data_result = explode(",", $rowData[0][13])
for($data_count=0;$data_count<count($data_result);$data_count++)
{
if(substr($data_result[$data_count], -1) == '-')
{
echo $data_result[$data_count]
}
}
?>

How to find similar text in a large string?

I have a large string str and a needle ndl. Now, I need to find similar text of ndl from the string str. For example,
SOURCE: "This is a demo text and I love you about this".
NEEDLE: "I you love"
OUTPUT: "I love you"
SOURCE: "I have a unique idea. Do you need one?".
NEEDLE: "a unik idia"
OUTPUT: "a unique idea"
I found that I can do this using similarity measures like cosine or manhatton similarity measure. However, I think implementation of this algorithms will be difficult. Would you please suggest me any easy or fastest way to do this maybe using any library function of php? TIA
There is no PHP native function to achieve this goal.However the possibilities of PHP is just limited by your imagination.We can't on SO suggest libraries to achieve your goal and you need to keep in mind that this kind of questions can be flagged as off-topic. So instead of suggesting some libraries I will just point you into the directions you need to explore.
As designed ,your question suggest that you don't need simple strings match functions like stripos and co and a regex can't achieve this. For examples
unik and unique
and also
idia and idea
can't be matched by those functions. So You need to look for something like levenshtein function.But as you need sub strings and not necessarly the whole string and also ,in order to make the work easier for the levenshtein function and your server, You need to use some imagination.You could for example break both haystack and needle in words and then use levenshtein to find most closest values to your needles.
This is one way to achieve this .Read carefully the comments to understand the idea and you will be able to implement something better.
for strings with only ASCII chars it is relatively easy to achieve it. But for other Encodings you will probably encounter many difficulties.But a simple approach to handle multibyte strings too could be something like:
function to_ascii($text,$encoding="UTF-8") {
if (is_string($text)) {
// Includes combinations of characters that present as a single glyph
$text = preg_replace_callback('/\X/u', __FUNCTION__, $text);
}
elseif (is_array($text) && count($text) == 1 && is_string($text[0])) {
// IGNORE characters that can't be TRANSLITerated to ASCII
$text = #iconv($encoding, "ASCII//IGNORE//TRANSLIT", $text[0]);
// The documentation says that iconv() returns false on failure but it returns ''
if ($text === '' || !is_string($text)) {
$text = '?';
}
elseif (preg_match('/\w/', $text)) { // If the text contains any letters...
$text = preg_replace('/\W+/', '', $text); // ...then remove all non-letters
}
}
else { // $text was not a string
$text = '';
}
return $text;
}
function find_similar($needle,$str,$keep_needle_order=false){
if(!is_string($needle)||!is_string($str))
{
return false;
}
$valid=array();
//get encodings and words from haystack and needle
setlocale(LC_CTYPE, 'en_GB.UTF8');
$encoding_s=mb_detect_encoding($str);
$encoding_n=mb_detect_encoding($needle);
mb_regex_encoding ($encoding_n);
$pneed=array_filter(mb_split('\W',$needle));
mb_regex_encoding ($encoding_s);
$pstr=array_filter(mb_split('\W',$str));
foreach($pneed as $k=>$word)//loop trough needle's words
{
foreach($pstr as $key=>$w)
{
if($encoding_n!==$encoding_s)
{//if $encodings are not the same make some transliteration
$tmp_word=($encoding_n!=='ASCII')?to_ascii($word,$encoding_n):$word;
$tmp_w=($encoding_s!=='ASCII')?to_ascii($w,$encoding_s):$w;
}else
{
$tmp_word=$word;
$tmp_w=$w;
}
$tmp[$tmp_w]=levenshtein($tmp_w,$tmp_word);//collect levenshtein distances
$keys[$tmp_w]=array($key,$w);
}
$nominees=array_flip(array_keys($tmp,min($tmp)));//get the nominees
$tmp=10000;
foreach($nominees as $nominee=>$idx)
{//test sound like to get more precision
$idx=levenshtein(metaphone($nominee),metaphone($tmp_word));
if($idx<$tmp){
$answer=$nominee;//get the winner
}
unset($nominees[$nominee]);
}
if(!$keep_needle_order){
$valid[$keys[$answer][0]]=$keys[$answer][1];//get the right form of the winner
}
else{
$valid[$k]=$keys[$answer][1];
}
$tmp=$nominees=array();//clean a little for the next iteration
}
if(!$keep_needle_order)
{
ksort($valid);
}
$valid=array_values($valid);//get only the values
/*return the array of the closest value to the
needle according to this algorithm of course*/
return $valid;
}
var_dump(find_similar('i knew you love me','finally i know you loved me and all my pets'));
var_dump(find_similar('I you love','This is a demo text and I love you about this'));
var_dump(find_similar('a unik idia','I have a unique idea. Do you need?'));
var_dump(find_similar("Goebel, Weiss, Goethe, Goethe und Goetz",'Weiß, Goldmann, Göbel, Weiss, Göthe, Goethe und Götz'));
var_dump(find_similar('Ḽơᶉëᶆ ȋṕšᶙṁ ḍỡḽǭᵳ ʂǐť ӓṁệẗ, ĉṓɲṩḙċťᶒțûɾ ấɖḯƥĭṩčįɳġ ḝłįʈ',
'Ḽơᶉëᶆ ȋṕšᶙṁ ḍỡḽǭᵳ ʂǐť ӓṁệẗ, ĉṓɲṩḙċťᶒțûɾ ấɖḯƥĭṩčįɳġ ḝłįʈ, șếᶑ ᶁⱺ ẽḭŭŝḿꝋď ṫĕᶆᶈṓɍ ỉñḉīḑȋᵭṵńť ṷŧ ḹẩḇőꝛế éȶ đꝍꞎôꝛȇ ᵯáꞡᶇā ąⱡîɋṹẵ.'));
and the output is:
array(5) {
[0]=>
string(1) "i"
[1]=>
string(4) "know"
[2]=>
string(3) "you"
[3]=>
string(5) "loved"
[4]=>
string(2) "me"
}
array(3) {
[0]=>
string(1) "I"
[1]=>
string(4) "love"
[2]=>
string(3) "you"
}
array(3) {
[0]=>
string(1) "a"
[1]=>
string(6) "unique"
[2]=>
string(4) "idea"
}
array(5) {
[0]=>
string(6) "Göbel"
[1]=>
string(5) "Weiss"
[2]=>
string(6) "Goethe"
[3]=>
string(3) "und"
[4]=>
string(5) "Götz"
}
array(8) {
[0]=>
string(13) "Ḽơᶉëᶆ"
[1]=>
string(13) "ȋṕšᶙṁ"
[2]=>
string(14) "ḍỡḽǭᵳ"
[3]=>
string(6) "ʂǐť"
[4]=>
string(11) "ӓṁệẗ"
[5]=>
string(26) "ĉṓɲṩḙċťᶒțûɾ"
[6]=>
string(23) "ấɖḯƥĭṩčįɳġ"
[7]=>
string(9) "ḝłįʈ"
}
if you need the output as string you can use join on the result of the function before use it
You can run the working code and check the result online
But you must keep in mind that this will not work for all kind of strings nor for all PHP versions
Try this code to find string within string
$data = "I have a unique idea. Do you need one?";
$find = "a unique idea";
$start = strpos($data, $find);
if($start){
$end = $start + strlen($find);
print_r(substr($data, $start, strlen($find)));
} else {
echo "not found";
}
This is a very easy way to do that:
$source = "This is a demo text and I love you about this";
$needle = "I you love";
$words = explode(" " , $source);
$needleWords = explode(" ", $needle);
$results = [];
foreach($needleWords as $key => $needleWord) {
foreach($words as $keyWords => $word) {
if(strcasecmp($word, $needleWord) == 0) {
$results[$keyWords] = $needleWord;
}
}
}
uksort($results, function($a , $b) {
return $a - $b;
});
echo(implode(" " , $results));
Output
I love you
Use this function it checks any length string
function sim($aa,$bb){
$l1 = strlen($aa);
$l2 = strlen($bb);
if ($l1 > $l2) {
$a = $bb;
$b = $aa;
}else{
$a = $aa;
$b = $bb;
}
// Format string
$a = explode(" ", implode(" ", array_unique(explode(" ", preg_replace("~\b[a-z]{1,2}\b\s*~", "", $a)))));
$b = explode(" ", implode(" ", array_unique(explode(" ", preg_replace("~\b[a-z]{1,2}\b\s*~", "", $b)))));
sort($a);
sort($b);
$a = implode(" ",$a);
$b = implode(" ",$b);
$a = strtolower(preg_replace("/[^A-Za-z0-9\-]/", " ", $a));
$b = strtolower(preg_replace("/[^A-Za-z0-9\-]/", " ", $b));
$dc2 = count(array_diff(str_split($a,2),str_split($b,2)));
$cA1 = count(str_split($a));
$cB1 = count(str_split($b));
$a = explode(" ",$a);
$b = explode(" ",$b);
$dc = count(array_diff($a, $b));
$cA = count($a);
$cB = count($b);
// Calculate similarity
$p = (((($cA + $cB) / 2) - $dc) * 100) / (($cA + $cB) / 2);
$p2 = ((((($cA1 / 2) + ($cB1 / 2)) / 2) - $dc2) * 100) / ((($cA1 / 2) + ($cB1 / 2)) / 2);
$percent = ($p + $p2) / 2;
echo $percent . " %";
}

PHP: How to get specific word from string

This is my string: $string="VARHELLO=helloVARWELCOME=123qwa";
I want to get 'hello' and '123qwa' from string.
My pseudo code is.
if /^VARHELLO/ exist
get hello(or whatever comes after VARHELLO and before VARWELCOME)
if /^VARWELCOME/ exist
get 123qwa(or whatever comes after VARWELCOME)
Note: values from 'VARHELLO' and 'VARWELCOME' are dynamic, so 'VARHELLO' could be 'H3Ll0' or VARWELCOME could be 'W3l60m3'.
Example:
$string="VARHELLO=H3Ll0VARWELCOME=W3l60m3";
Here is some code that will parse this string out for you into a more usable array.
<?php
$string="VARHELLO=helloVARWELCOME=123qwa";
$parsed = [];
$parts = explode('VAR', $string);
foreach($parts AS $part){
if(strlen($part)){
$subParts = explode('=', $part);
$parsed[$subParts[0]] = $subParts[1];
}
}
var_dump($parsed);
Output:
array(2) {
["HELLO"]=>
string(5) "hello"
["WELCOME"]=>
string(6) "123qwa"
}
Or, an alternative using parse_str (http://php.net/manual/en/function.parse-str.php)
<?php
$string="VARHELLO=helloVARWELCOME=123qwa";
$string = str_replace('VAR', '&', $string);
var_dump($string);
parse_str($string);
var_dump($HELLO);
var_dump($WELCOME);
Output:
string(27) "&HELLO=hello&WELCOME=123qwa"
string(5) "hello"
string(6) "123qwa"
Jessica's answer is perfect, but if you want to get it using preg_match
$string="VARHELLO=helloVARWELCOME=123qwa";
preg_match('/VARHELLO=(.*?)VARWELCOME=(.*)/is', $string, $m);
var_dump($m);
your results will be $m[1] and $m[2]
array(3) {
[0]=>
string(31) "VARHELLO=helloVARWELCOME=123qwa"
[1]=>
string(5) "hello"
[2]=>
string(6) "123qwa"
}

split a comma separated string in a pair of 2 using php

I have a string having 128 values in the form of :
1,4,5,6,0,0,1,0,0,5,6,...1,2,3.
I want to pair in the form of :
(1,4),(5,6),(7,8)
so that I can make a for loop for 64 data using PHP.
You can accomplish this in these steps:
Use explode() to turn the string into an array of numbers
Use array_chunk() to form groups of two
Use array_map() to turn each group into a string with brackets
Use join() to glue everything back together.
You can use this delicious one-liner, because everyone loves those:
echo join(',', array_map(function($chunk) {
return sprintf('(%d,%d)', $chunk[0], isset($chunk[1]) ? $chunk[1] : '0');
}, array_chunk(explode(',', $array), 2)));
Demo
If the last chunk is smaller than two items, it will use '0' as the second value.
<?php
$a = 'val1,val2,val3,val4';
function x($value)
{
$buffer = explode(',', $value);
$result = array();
while(count($buffer))
{ $result[] = array(array_shift($buffer), array_shift($buffer)); }
return $result;
}
$result = x($a);
var_dump($result);
?>
Shows:
array(2) { [0]=> array(2) { [0]=> string(4) "val1" [1]=> string(4) "val2" } [1]=> array(2) { [0]=> string(4) "val3" [1]=> string(4) "val4" } }
If modify it, then it might help you this way:
<?php
$a = '1,2,3,4';
function x($value)
{
$buffer = explode(',', $value);
$result = array();
while(count($buffer))
{ $result[] = sprintf('(%d,%d)', array_shift($buffer), array_shift($buffer)); }
return implode(',', $result);
}
$result = x($a);
var_dump($result);
?>
Which shows:
string(11) "(1,2),(3,4)"

Categories