w3resource

PHP Regular Expression Exercises: Extract text (within parenthesis) from a string

PHP regular expression: Exercise-6 with Solution

Write a PHP script to extract text (within parenthesis) from a string.

Sample strings : 'The quick brown [fox].'

Pictorial Presentation:

PHP Regular Expression Exercise: Extract text (within parenthesis) from a string

Sample Solution:

PHP Code:

<?php
$my_text = 'The quick brown [fox].';
preg_match('#\[(.*?)\]#', $my_text, $match);
print $match[1]."\n";
?>

Sample Output:

fox

Flowchart :

Flowchart: PHP Regular Expression Exercise - Extract text (within parenthesis) from a string

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP script to remove new lines (characters) from a string.
Next: Write a PHP script to remove all characters from a string except a-z A-Z 0-9 or " ".

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.