I'm trying to split a php string in to parts the first one include the delimiter but the second one doesn't
$string = "abc==123";
What I want exactly is to get
$string['0'] = "abc==";
$string['1'] = "123";
Thanks for help
Simple enough
<?php
$string = explode("==", $string);
$string[0] .= "==";
?>
I believe you want the function strstr
http://us1.php.net/strstr
You can use PHP's explode function,
$data = "abc==123";
$result = explode("==", $data);
echo $result[0]. "==";
echo $result[1];
Related
I am getting a unique issue. I have string like "Home[||](|i am in here|)[||]". Now i want to replace everything after first occurance from left of '[||]' with null. Tried many ways like pregreplace,substring,explode,strstr,strreplace... But nothing seems to work out. Can someone please help.
Using strstr() would be about the simplest...
echo strstr("Home[||](|i am in here|)[||]", "[||]", true);
You can use regex to find the part of the string you want to replace.
<?php
$input = "Home[||](|i am in here|)[||]";
$pattern = "/\[\|\|\](.*)/";
$output = preg_replace($pattern, "", $input);
echo $output;
?>
You can do it by many way using strstr(), strtok(), preg_replace() etc. But I would prefer with explode(), without any kind of regex or str_* function to achieve what you want. Hope this will help to get your job done.
echo explode('[||]',"Home[||](|i am in here|)[||]")[0];
DEMO: https://eval.in/1040308
With strtok()
<?php
$mystring = 'Home[||](|i am in here|)[||]';
$first = strtok($mystring, '[||]');
echo $first; // Home
?>
With strstr():
<?php
$mystring = 'Home[||](|i am in here|)[||]';
$first = strstr($mystring, "[||]", true);
echo $first; // Home
?>
My string:
'KCP-PRO;first_name last_name;address;zipcode;country' //for example: 'KCP-PRO;Jon Doe;Box 564;02201;USA'
or
'KCP-SAT-PRO;first_name last_name;address;zipcode;country'
How can i change the first part (KCP-PRO or KCP-SAT-PRO) and change it to (KCP,PRO or KCP,SAT,PRO)? The outcome has to be:
'KCP,PRO;first_name last_name;address;zipcode;country'
or
'KCP,SAT,PRO;first_name last_name;address;zipcode;country'
I haven't tried the code myself but I guess this will do the trick
$string = 'KCP-SAT-PRO;first_name last_name;address;zipcode;country';
$stringExploded = explode(';', $string);
$stringExploded[0] = str_replace('-', ',', $stringExploded[0]);
$output = implode(';', $stringExploded);
//output should be KCP,SAT,PRO;first_name last_name;address;zipcode;country
Hope this helps :)
Or you can use preg_replace_callback function with the following regex
^[^;]*
So your code looks like as
echo preg_replace_callback("/^[^;]*/",function($m){
return str_replace("-",',',$m[0]);
},"KCP-SAT-PRO;first_name last_name;address;zipcode;country");
I'm new to PHP and I have a problem.
I need delete all chars since a symbol (Sorry for my bad english , i'm from argentina)
I have this text :
3,94€
And I need the text is as follows:
3,94
I tried this by multiple ways but it didn't work.
There are a few ways you can do this:
Using strpos:
$string = '3,94€';
echo substr($string, 0, strpos($string, '&'));
or using strstr:
// Requires PHP 5.3+ due to the true (before_needle) parameter
$string = '3,94€';
echo strstr($string, '&', true);
or using explode:
// Useful if you need to keep the € part for later
$string = '3,94€';
list($part_a, $part_b) = explode('&', $string);
echo $part_a;
or using reset:
$string = '3,94€';
echo reset(explode('&', $string));
The best suited in your case would be to use strpos to find the first occurrence of & in the string, and then use substr to return the string from the begining until the value returned by strpos.
Another posibility is clean the number and then round it:
<?php
//Option 1: with regular expresions:
$val = '3,94€';
$res = preg_replace('/[^0-9.,]/','',$val);
var_dump($res);
//Option 2: with filter functions:
$res2 = filter_var($val, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND);
var_dump($res2);
//Output: 3,948364
//If you want to round it:
$res = substr($res, 0, 4);
var_dump($res);
?>
You can use regex: https://regex101.com/r/uY0kH3/1
It will work in preg_match() function.
You could use str_replace.
<?php
$string = 3,94€
$final = str_replace('€' ,'', $string);
echo $final;
I have string that will look like this:
$string = "hello, my, name, is, az";
Now I just wanna echo whatever is there before first comma. I have been using following:
echo strstr($this->tags, ',', true);
and It has been working great, but the problem it only works php 5.3.0 and above. I am currently on PHP 5.2.
I know this could be achieve through regular express by pregmatch but I suck at RE.
Can someone help me with this.
Regards,
<?php
$string = "hello, my, name, is, az";
echo substr($string, 0, strpos($string, ','));
You can (and should) add further checks to avoid substr if there's no , in the string.
Use explode than,
$arr = explode(',', $string,2);
echo $arr[0];
You can explode this string using comma and read first argument of array like this
$string = "hello, my, name, is, az";
$str = explode(",", $string, 2);
echo $str[0];
$parts = explode(',',$string);
echo $parts[0];
You can simple use the explode function:
$string = "hello, my, name, is, az";
$output = explode(",", $string);
echo $output[0];
Too much explosives for a small work.
$str = current(explode(',', $string));
I have three variables here.
$first= 'Start';
$second = 'End';
$testVar = 'Start here and here until the End more more more strings here';
How can i search $testVar if it contains Start and End strings, and i want to post Start up to the End string only.
Another way is using substr() and strops()
substr($testVar,strpos($testVar,$first),strpos($testvar,$second)+3)
Looking at your previous posts I'm assuming this is a PHP question. If so you can use:
list(,$result) = explode($first,$testVar);
list($result) = explode($second,$result);
Codepad link
You can also use regex as:
$first = preg_quote($first);
$second = preg_quote($second);
if(preg_match("!$first(.*?)$second!",$testVar,$m)) {
echo $m[1];
}
Codepad link
I would also rather use strpos, strlen and substr:
$end_pos = strpos($testVar,$end)+strlen($end);
$result = substr($testVar, strpos($testVar,$start), $end_pos );