PHP Regular Expression Doubt

Friends,

I have silly doubts:

Suppose I have a line like this

Heading: Value1; SomeText1 (a, b, c), Value 2; SomeText2 (d, e, f)

I wanted to remove all semicolons and delete everything in brackets (including brackets). I managed to do this using this code

if (strstr($line,'Heading')){
                $new_heading = str_replace(";", "", $line); // Replaces semi-colon
                $new_heading = preg_replace("/\([^\)]+\)/","",$new_heading); //Removes Text With in Brackets
                $line = $new_heading;
                echo $line; //Outputs "Heading: Value1 SomeText1 , Value 2 SomeText2"
                } 

Now suppose I have a line like this

Heading: Text1 (a, b) Text2. (d, f) Text3 (g, h)

What I want to achieve is to remove everything in brackets (parentheses included) and replace it with a comma. However, the last bracket condition should not be replaced with a comma.

I mean, the conclusion should be

Heading: Text1 , Text2. , Text3

How to achieve this?

+3
source share
7 answers

Can you use two expressions?

$text = "Heading: Text1 (a, b) Text2. (d, f) Text3 (g, h)";

$new = preg_replace("/\([^)]*\)(?=.*?\([^)]*\))/",",",$text);
$new = preg_replace("/\([^)]*\)/","",$new);

echo $new . "\n";

, - . (g, h) . ( ) .

0

, substr...

$newstr = substr($str, 0, strlen($str)-1);  

- ...

: > , ... ?

$new_heading = preg_replace("/\([^\)]+\)/",",",$new_heading);
$newstr = substr($new_heading, 0, strlen($str)-1);  

EDIT: > . :) , RegxLib

+1

() ,

$text = "Heading: Text1 (a, b) Text2. (d, f) Text3 (g, h)";

preg_match_all("/\([^\)]+\)/",$text, $brackets);

$bracket_c = count($brackets);

for($bracket_i = 0; $bracket_i < $bracket_c; $bracket_i += 1){
    if($bracket_i == $bracket_c - 1){
        $text = str_replace($brackets[$bracket_i], "", $text);
    }else{
        $text = str_replace($brackets[$bracket_i], ",", $text);
    }
}
echo $text . "\n";
+1

preg_replace(), $limit. , :

  • preg_match_all
  • - 1 preg_replace
  • preg_replace,

:

preg_match_all("/\([^\)]+\)/",$new_heading,$matches);
$new_heading = preg_replace("/\([^\)]+\)/",",",$new_heading, count($matches) - 1);
$new_heading = preg_replace("/\([^\)]+\)/","",$new_heading);

:

  • preg_replace, , . $count, .
  • - 1 preg_replace
  • preg_replace,

:

preg_replace("/\([^\)]+\)/","",$new_heading, null, $count);
$new_heading = preg_replace("/\([^\)]+\)/",",",$new_heading, $count - 1);
$new_heading = preg_replace("/\([^\)]+\)/","",$new_heading);
+1
<?php
$line = 'Heading: Text1 (a, b) Text2. (d, f) Text3 (g, h)';
$line = substr(preg_replace('/\([^\)]+\)/', ',', $line), 0, -1);
?>

, :

<?php
$line = 'Heading: Text1 (a, b) Text2. (d, f) Text3 (g, h)';
$line = preg_replace('/ \([^\)]+\)$/', '', $line);
$line = preg_replace('/\([^\)]+\)/', ',', $line);
?>

. .

0

, .

  • preg_match, , n
  • preg_replace, n-1 limit n-1
  • use preg_replaceto replace the set of brackets with an empty string
0
source

Use the following code:

$str = 'Text1 (a, b) Text2. (d, f) Text3 (g, h)';
$arr = preg_split('~\([^)]*\)~', $str, -1 , PREG_SPLIT_NO_EMPTY);
var_dump(implode(',', $arr));

OUTPUT

string(23) "Text1 , Text2. , Text3 "
0
source

All Articles