Removing all characters before certain string [duplicate] - php

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

Delete text between two strings [duplicate]

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

PHP convert string to "\xnn" hex [duplicate]

This question already has answers here:
PHP convert string to hex and hex to string
(8 answers)
Encoding/decoding string in hexadecimal and back
(2 answers)
Closed 4 years ago.
How can i convert string 'Hello world' to '\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21' using php?
Thanks!
Should work
function strhex($string) {
$hexstr = unpack('H*', $string);
return array_shift($hexstr);
}
Update
Code below what you need
function strtohex($string)
{
$string = str_split($string);
foreach($string as &$char)
$char = "\x".dechex(ord($char));
return implode('',$string);
}
print strtohex("[0-9A-Za-z\+/=]*");

Remove everything right of a match in a variable in PHP [duplicate]

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

PHP ltrim() not working as expected [duplicate]

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);

PHP: What is the correct pattern for this? (preg_match) [duplicate]

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;

Categories