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
?>
Related
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 am trying to replace the following text
options":{"icon": "color.php?tx=31c},
into
options":{"icon": "color.php?tx=31*},
basically removing the 'c' and replacing it with an asterisk
and the php function i'm using is as simple as a str_replace. So my code is as follow
$str = 'options":{"icon": "color.php?tx=31c},';
$newStr = str_replace("c},","*},",$str);
but for some reason when i echo $newstr, the str_replace didn't replace anything. I tried with just c, it works. I tried with just the }, it works. But not together. Anyone has any idea?
Semicolon (;) missing on first line. Should be:
$str = 'options":{"icon": "color.php?tx=31c},';
<?php
$str = 'options":{"icon": "color.php?tx=31c},';
$newStr = str_replace("c},","*},",$str);
echo $newStr;
This code works perfectly for me. See this link : https://ideone.com/wOTb8V
As you can see, the output is options":{"icon": "color.php?tx=31*},
Maybe you are trying to echo $str instead of $newStr ?
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];
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));
How should one use preg_replace() to replace a string from 'aabbaacc' to 'abc'?
Currently, my code uses str_split() then array_unique() then implode().
I think preg_replace() can achieve this also, but I don't know how.
Thank you for your help.
A regex that seems to work for me is /(.)(?=.*?\1)/. Please test it for yourself here:
http://regexpal.com/
I've also tested it with preg_replace('/(.)(?=.*?\1)/', '', 'aaabbbabc') which returns the expected abc.
Hope this helps :)
This is the closest I got. However, it's basically a copy of :
How do I remove duplicate characters and keep the unique one only in Perl?
<?php
$string = 'aabbaacc';
$new = preg_replace( '/(.)(?=.*?\1)/i','', $string );
echo $new;
?>
Unfortunately, it does not keep the string in the same order. I don't know if that is important to you or not.
try this
$string = 'dbbaabbbaac';
$new = preg_replace_callback( array("/(.)\\1+/"),function($M){print_r($M);return $M[1];}, $string );
$new = preg_replace_callback( array('/(.)(.?\\1)/i','/(.)(.*?\\1)/i'),function($M){return $M[1].trim($M[2],$M[1]);}, $new );
echo $new."\n";
output
dbac
or this with out Regex
$value="aabbaacc";
for($i=0;$i<strlen($value);$i++){
$out[$value[$i]]=$value[$i];
}
echo implode("",$out);
output:
abc