0

I have this code from which I like to return an array that holds all hits for patterns that starts with 'some' and ends with 'string'.

$mystr = "this string contains some variables such as  $this->lang->line('some_string') and $this->lang->line('some_other_string')";
preg_match_all ("/\bsome[\w%+\/-]+?string\b/", $mystr, $result);

However I like to have all hits that starts with

$this->lang->line('

and ends with

')

Furthermore, I need to have the start and end patterns left out. In other words, I like to see 'some_string' and 'some_other_string' in my resulting array. Replacing 'some' and 'string' straight forward is not working, because of special characters?

  • What about [strpos](http://php.net/manual/en/function.strpos.php) ? If you find the position, look for closing tag and take everything in between. – Peon Mar 28 '17 at 09:07
  • Next time go to https://regex101.com/ and try to self solve before posting the question. Trying is the best way to learn. This was a very basic question and you knew exactly what you needed to search between. – mickmackusa Mar 28 '17 at 09:17
  • Not to basic for me. Thanks for the link, I will practice. – user3104427 Mar 28 '17 at 15:50

2 Answers2

0
$mystr = "this string contains some variables such as  $this->lang->line('some_string') and $this->lang->line('some_other_string')";

preg_match_all("/\$this->lang->line\('(.*?)'\)/", $mystr, $result);

Output:

array(1
    0   =>  array(2
                0   =>  $this->lang->line('some_string')
                1   =>  $this->lang->line('some_other_string')
            )
    1   =>  array(2
                0   =>  some_string
                1   =>  some_other_string
            )

)
Hossam
  • 1,126
  • 8
  • 19
0

Here an example that escapes the special chars for you:

$mystr = "this string contains some variables such as \$this->lang->line('some_string') and \$this->lang->line('some_other_string')";
#array of regEx special chars
$regexSpecials = explode(' ',". ^ $ * + - ? ( ) [ ] { } \\ |");

#test string 1
#here we have the problem that we have $ and ', so if we use
# single-quotes we have to handle the single-quote in the string right.
# double-quotes we have to handle the dollar-sign in the string right.
$some = "\$this->lang->line('";

#test string 2
$string = "')";

#escape   chr(92) means \ 
foreach($regexSpecials as $chr){
   $some = str_replace($chr,chr(92).ltrim($chr,chr(92)),$some);
   $string = str_replace($chr,chr(92).ltrim($chr,chr(92)),$string);
}

#match 
preg_match_all ('/'.$some.'(.*?)'.$string.'/', $mystr, $result);

#show
print_r($result);

The hard part is to escape everthing right in php and in the regexstring.

  • You have to escape the dollar sign in php right when used in double-quotes
  • Also you have escape all special characters right for the regex.

Read more here:

What special characters must be escaped in regular expressions?

What does it mean to escape a string?

Community
  • 1
  • 1
JustOnUnderMillions
  • 3,741
  • 9
  • 12