Extract specific substring from a string

Hi,

Hope all are well. I wanna know one simple thing, may be simple but I don’t know.

Explain by example: Say we have a string like as follows:

$str = 'This is an article{content inside second bracket} that is related to {content inside second bracket} PHP';

How can I detect and extract the part inside second bracket [content inside second bracket]?

Any idea to achieve it?? preg_match() or anything else.

Thanks for seeing my post.
Rashidul


<?php
$string = 'This is an article{content inside first bracket} that is related to {content inside second bracket} PHP';

preg_match_all(
  '~(?<={).+?(?=})~',
  $string,
  $matches
);

print_r(
  $matches
);

/*
  Array
  (
    [0] => Array
    (
      [0] => content inside first bracket
      [1] => content inside second bracket
    )
  )
*/

:slight_smile:

Thanks Anthony for ur Quick response. I appreciate ur effort.

Hi,

If i want to replace the actual string looks like

$string = 'This is an article{#1} that is related to {#2} PHP';

First match: #1
2nd Match: #2
and so on.

How can I do this?

Anthony’s code works just as well with the new string as it did with the old string.

Try it:


$string = 'This is an article{#1} that is related to {#2} PHP'; 

preg_match_all(
  '~(?<={).+?(?=})~',
  $string,
  $matches
);

print_r(
  $matches
);

/*
Array
(
    [0] => Array
        (
            [0] => #1
            [1] => #2
        )

)
*/

Actually I want to do is as follows :slight_smile:

Actual string will looks like as

$string = 'This is an article{content inside first bracket} that is related to {content inside second bracket} PHP'; 

After preg_match_all I want to see the string looks like as

$string = 'This is an article{#1} that is related to {#2} PHP'; 

Is it possible?

After preg_match_all or using preg_match_all I wanna do this

So do you want to extract the characters between {} and replace them with something else or do you want to capture the characters between {} and do something absolutely different with those? The code provided works for capturing the substrings, and if you got stuck with replacing those with something else then you need to articulate yourself a bit clearer.

I want to replace the above string with numbering. First match will get number 1, second match will get number 2 and so on…
Sample input:

$string = 'This is an article{content inside first bracket} that is related to {content inside second bracket} PHP';

Sample output:

 echo $string; // output : This is an article{#1} that is related to {#2} PHP

$string = 'This is an article{content inside first bracket} that is related to {content inside second bracket} PHP';

preg_match_all(
  '~(?<={).+?(?=})~',
  $string,
  $matches
);

$i = 1;

function do_increment(&$i)
{
	return '#' . $i++;
}

$string = preg_replace("~(?<={).+?(?=})~e", 'do_increment($i)', $string);

var_dump($string);

Thanks. perfect!

However would u plz suggest me from where I can learn the regular expression?? I know PHP programming very well but regex.

I don’t want to sound rude, but if someone showed you how to extract portion of a string, and if you couldn’t reach a conclusion how to change that substring to something else - you might want to work more with your php because this can be done with looping trough $matches and using str_replace, which we can all agree - are basics of string manipulation. :slight_smile:

If you want to learn how to create your own regular expressions, good place to start is www.regular-expressions.info and of course, http://www.php.net/manual/en/ref.pcre.php.

Good luck :slight_smile:

I actually wanted to do using regex. I know string manipulation.

Thanks for ur suggestion.

Hi,

If I want to do something like as follows [second bracket also be removed]:
sample input:

$string = 'This is an article{content inside first bracket} that is related to {content inside second bracket} PHP'; 

Output:

echo $string; // output : This is an article #1 that is related to #2 PHP  

How can I do this. Any idea??

The code provided by furicane in post #10 does exactly what you want. Have you tried that code?

It gives output looks like:

 This is an article {#1} that is related to {#2} PHP 

I need to remove the second bracket also.

It should looks like:

This is an article #1 that is related to #2 PHP