This question already has answers here:
PHP remove all characters before specific string
(4 answers)
Closed 8 years ago.
How can I remove everything before the first ABC in a string?
So that this:
somethingrandomABCotherrandomstuffABCmorerandomstuffABC
turns to this:
otherrandomstuffABCmorerandomstuffABC.
Is this possible with php?
There is a php built in for doing this: strstr
Combine with substr to strip out your token:
$out = substr(strstr($text, 'ABC'), strlen('ABC'))
<?php
function removeEverythingBefore($in, $before) {
$pos = strpos($in, $before);
return $pos !== FALSE
? substr($in, $pos + strlen($before), strlen($in))
: "";
}
echo(removeEverythingBefore("somethingrandomABCotherrandomstuffABCmorerandomstuffABC", "ABC"));
?>
Outputs:
otherrandomstuffABCmorerandomstuffABC
Related
This question already has answers here:
Strip off specific parameter from URL's querystring
(22 answers)
Closed 3 years ago.
I'm triying to delete the text between include and the next & in the text:
filter[mat]=1&filter[status]=available&include=homeCompetitor,awayCompetitor&sort=scheduled
and get a text like:
filter[mat]=1&filter[status]=available&sort=scheduled
I tried with the following codes:
function delete_all_between($string)
{
$beginningPos = strpos($string, $beginning);
$endPos = strpos($string, $end);
if ($beginningPos === false || $endPos === false)
return $string;
}
and also with:
preg_replace("/\include[^]+\&/","",$string);
But none worked.
It may also be that the text also has the following format:
filter[mat]=1&filter[status]=available&sort=scheduled&include=homeCompetitor,awayCompetitor
With which I need to delete from the word include to the end (in this case there is no &)
EDIT:
This question isn't duplicated because that answer didn't work for me!
The string you are dealing with is in a particular format that can be parsed by parse_str and reassembled via http_build_query.
<?php
$str = "filter[mat]=1&filter[status]=available&include=homeCompetitor,awayCompetitor&sort=scheduled";
parse_str($str, $output);
unset($output["include"]);
echo http_build_query($output);
Try this :
preg_replace('/(include(.*)\&)|(\&include([^&]*)$)/i', '', $string)
I tested with :
filter[mat]=1&filter[status]=available&include=homeCompetitor,awayCompetitor&sort=scheduled
Result : filter[mat]=1&filter[status]=available&sort=scheduled
filter[mat]=1&filter[status]=available&include=homeCompetitor,awayCompetitor
Result : filter[mat]=1&filter[status]=available
filter[mat]=1&filter[status]=available&INCLUDE=homeCompetitor,awayCompetitor&sort=scheduled
Result : filter[mat]=1&filter[status]=available&sort=scheduled
This question already has answers here:
removing #email.com from string in php
(6 answers)
Closed 4 years ago.
How can I use the str_replace() without using an array of words for turning:
some#mail.com
into
some
So, everything after the '#' sign includes # as well.
How can I do that?
As an example, i will type 'adminofsite#xxxwwweeerandomstuff.com'
and the output will be: 'adminofsite'.
use strstr
$email="john#doe.com"
$user = strstr($email, '#', true);
echo $user;
$str = "some#mail.com"
$str = substr($str,0,strpos($str,"#"))
function stripEmailDomain($string){
return substr($string, 0, strpos($string, '#'));
}
I exploded and then rebuilt the string. This will work if you are positive the string will always be an email address. Its a work around but seems flexible if you want to modify it for a specific purpose.
<?php
$explo0= explode('#',"some#mail.com");
for($i=0;$i<count($explo0);$i++){
$exploresult0.=$explo0[$i+1];
}
echo "#".$exploresult0;
?>
This question already has answers here:
PHP remove all characters before specific string
(4 answers)
Closed 8 years ago.
How can I remove everything before the first ABC in a string?
So that this:
somethingrandomABCotherrandomstuffABCmorerandomstuffABC
turns to this:
otherrandomstuffABCmorerandomstuffABC.
Is this possible with php?
There is a php built in for doing this: strstr
Combine with substr to strip out your token:
$out = substr(strstr($text, 'ABC'), strlen('ABC'))
<?php
function removeEverythingBefore($in, $before) {
$pos = strpos($in, $before);
return $pos !== FALSE
? substr($in, $pos + strlen($before), strlen($in))
: "";
}
echo(removeEverythingBefore("somethingrandomABCotherrandomstuffABCmorerandomstuffABC", "ABC"));
?>
Outputs:
otherrandomstuffABCmorerandomstuffABC
This question already has answers here:
PHP ltrim behavior with character list
(2 answers)
Closed 8 years ago.
I have this code..
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = ltrim($homepage1, 'datastring=/mac_project');
echo $trimmed;
I get the output as folio/kitchen/images/03.jpg. It's missing the /port from the /portfolio directory.
Full output should've been /portfolio/kitchen/images/03.jpg
Why not do the simple str_replace() ?
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = str_replace('datastring=/mac_project','',$homepage1);
echo $trimmed;// "prints" /portfolio/kitchen/images/03.jpg
The second parameter for ltrim is for character_mask, which means all the chars in the list will be trimmed.
You could use str_replace(), or if you want to replace only at the beginning of the string by preg_replace():
$trimmed = preg_replace('~^datastring=/mac_project~', '', $homepage1);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regex, get string value between two characters
myemail#gmail.com
What is the pattern to get what's between '#' and '.' in this string?
In this case it would get 'gmail'.
I believe it is ...
preg_match("'#(.*?)\.'si", $source, $match);
$email = 'myemail#gmail.com';
$gmail = preg_match('/#([^\.]*)\./', $email, $m) ? $m[1] : false;