Delete text between two strings [duplicate] - php

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

Related

Laravel: Regex to strip part of a url [duplicate]

This question already has answers here:
extract part of a path with filename with php
(2 answers)
Closed 2 years ago.
$string = "https:\/\/s3.us-west-2.amazonaws.com\/dev-lopes\/videos\/585903773a9_654641765275736"
My intention is to have the videos path (stripping the escaped forward slash) and the identifier set to a variable, like videos/585903773a9_654641765275736
Is this possible using str_replace or is this strictly regex required?
Try this Way. I think it will help you. I hope you will get your desire output.
<?php
// Your code here!
$string = "https:\/\/s3.us-west-2.amazonaws.com\/dev-lopes\/videos\/585903773a9_654641765275736";
//remove back slashes
$str1 = stripslashes($string);
//count total string length.
$len = strlen($str1);
//pick the position of /videos
$pos = strpos($str1,'/vid');
//split it from the url.
$break_string = substr($str1, $pos, $len);
//output: /videos/585903773a9_654641765275736
echo($break_string);
?>

use str_replace to remove after # [duplicate]

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

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

Removing all characters before certain string [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

How to get a second specific word inside a string in PHP [duplicate]

This question already has answers here:
Question about strpos: how to get 2nd occurrence of a string?
(15 answers)
Closed 9 years ago.
Hey guys so I have a question,
Lets say someone puts "Hi lets have #fun and than more #fun" Now lets say I want to grab the second hashtag including the word connected to it and store it into a variable.
How would I go about getting the second part because using (strpos($a,'#fun') would only return the first one.
Thank you for your time!
David
Edit:
Here's what I did:
$code = "Hi lets have #funfgs and than more #funny";
$pos1 = strpos($code, '#');
$pos2 = strpos($code, '#', $pos1 + strlen('#'));
echo substr($code, $pos2);
You can use following Regular Expression
$code = "Hi lets have #fun and than more #fun";
$pattern = "$#[^\s]*$i";
preg_match_all($pattern, $code, $matches);
print_r($matches);

Categories