-1

When using PHP, how can I remove the characters between two forward slashes in a $var?

I tried:

$test = "gdgfdgdf/asg8&7_09()9/87iuyiuuyty";
$test = str_replace('/\s+/', '_', $test);

Note that /asg8&7_09()9/ is dynamic data.

Unfortunately this doesn't give the intended results. How can I accomplish this using PHP?

2

2 Answers 2

1

Your question isn't exactly clear on if you want to remove just what is between the slashes, or the slashes as well.

$test = "gdgfdgdf/asg8&7_09()9/87iuyiuuyty";
$test = preg_replace( '/\/.*?\//', '_', $test );
print_r( $test );

Result

gdgfdgdf_87iuyiuuyty

This code removes the slashes and their content. For the slashes to persist after processing, you could make the replacement '/_/'

$test = preg_replace( '/\/.*?\//', '/_/', $test );

Result

gdgfdgdf/_/87iuyiuuyty


Within the match pattern

. Match any character (except newline)
* Match 0 or more times

Regex Pal demonstration

0

Use preg_replace since it's dynamic data.

$test = "gdgfdgdf/asg8&7_09()9/87iuyiuuyty";
$test = preg_replace("/\/(.*?)\//",'_',$test);

Will return "gdgfdgdf_87iuyiuuyty"

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.