2
$string = 'Justin Timberlake (One Direction)';
$string = str_replace('????', '',$string);

I want everything within () to be removed, include the bracket, is it possible with str_replace? Sometime it could be like this ( fff). regex is harder to maintain so I avoid that.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
user3522742
  • 51
  • 1
  • 6
  • 1
    I would still use regex for this. Or split the string, replace, rejoin the strings. – ʰᵈˑ Sep 16 '14 at 16:21
  • Could possibly use a combination of `strpos` to find the opening `(`, then `strpos` again to find the closing `)` and join two `substr` from 0 to `(` and from `)` to end. – Jonathon Sep 16 '14 at 16:26

3 Answers3

2

I don't think str_replace is feasible here, as it searches for a specific string to replace, and we cannot give it a specific string to replace, as (One Direction) can change. (I'm assuming it can change, and therefore not a viable option). Here's two alternatives;

Not using regular expressions

<?php

$string = "Justin Timberlake (One Direction)";
echo substr($string, 0, strpos($string, "("));

https://eval.in/194261

Using regular expressions

<?php

$string = "Justin Timberlake (One Direction)";
$string = preg_replace("/\(.*\)/", "", $string);
echo $string;

https://eval.in/194255

ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49
0

I would use preg_replace like in this question: Remove Text Between Parentheses PHP

$string = "ABC (Test1)";
echo preg_replace("/\([^)]+\)/","",$string);

Edit:

$string = "ABC (Test1)";
echo preg_replace("/\([^)]*\)/","",$string);
Community
  • 1
  • 1
Bart Scheffer
  • 497
  • 1
  • 3
  • 18
  • This will fail. If the brackets are empty (ie: `()`), then the brackets won't be removed. "**include the bracket**". The `+` means match 1 or more of the preceding token. Change that to `*` – ʰᵈˑ Sep 16 '14 at 16:30
  • You're right, that works better in cases there's nothing between the () – Bart Scheffer Sep 16 '14 at 16:35
  • If I have `(One )Direction))`, it will only replace `(One )` with an empty string, leaving `Direction))`, which still fails OPs requirements. - doesn't it? – ʰᵈˑ Sep 16 '14 at 16:37
  • It does but in this case this is the answer for his question, you can always use str_replace again to delete all () which are still in the string – Bart Scheffer Sep 16 '14 at 16:46
0

if you want to change content inside ()

https://eval.in/194270

<?php

$string = "Justin Timberlake (One Direction)";
$start = '(';
$end = ')';
if (strpos($string,$end) !== false) {
            $startpos = strpos($string, $start) + strlen($start);
            if (strpos($string, $start) !== false) 
            {
            $endpos = strpos($string, $end, $startpos);
                if (strpos($string, $end, $startpos) !== false)
                {
                  $data_replace= substr($string, $startpos, $endpos - $startpos);
                }               
            }
   }
$data_insert = "Sing Song";
echo $data_updated = str_replace($data_replace,$data_insert,$string);   
?>
user229044
  • 232,980
  • 40
  • 330
  • 338
Rishabh
  • 546
  • 5
  • 8