user580523
user580523

Reputation:

Simple strip in PHP

I'm looking for a really simple of way stripping content from this string:

Example:

/m/NEEDED/

I would like to strip everything except "NEEDED".

Any help would be great, Thanks!

Upvotes: 0

Views: 91

Answers (3)

Christian
Christian

Reputation: 28164

The str_replace() one is quite specific, and the one I'd use.

But since you mentioned strip(), it reminded me of trim():

echo trim('/m/NEEDED/','/m');

Example: http://codepad.viper-7.com/AcDZCM

Upvotes: 1

Warface
Warface

Reputation: 5119

<?php
   $strip = array("/","m");
   echo str_replace($strip,"","text input here");
?>

Add more characters that you want to strip in the array if you want.

Upvotes: 0

genesis
genesis

Reputation: 50982

$str = "/m/NEEDED/";
$newStr = preg_replace("~(/m/|/)~", "", $str);

demo

OR

$str = "/m/NEEDED/";
$newStr = preg_replace("|/(.*?)/(.*?)/|", "$2", $str);

demo

OR

$str = "/m/NEEDED/";
$newStr = str_replace(array('/', 'm'), "", $str);

demo

Upvotes: 0

Related Questions