Get content from bracket - php

With preg_match how can I get the string between the bracket
Example: sdsdds (sdsd) sdsdsd
And I want the
sdsd

preg_match('/\(([^\)]*)\)/', 'sdsdds (sdsd) sdsdsd', $matches);
echo $matches[1]; // sdsd
Matches characters within parentheses, including blank values. If you want to match multiple instances, you can use preg_match_all.

preg_match('/\((.*?)\)/', $text, $a);
echo $a[1];

The simplest:
#\(([^\)]+)\)#
It's not very readable, because all the ( and ) must be escaped with \.
The # are delimiters.
Using preg_match:
$str = 'sdsdds (sdsd) sdsdsd';
$iMatches = preg_match('#\(([^\)]+)\)#', $str, $aMatches);
echo $aMatches[1]; // 'sdsd'

Related

Dot (.) Meta character Php Regex

Dot (.) Meta character is not matching text and showing zero(o) output. Please tell me what i am missing in this code.
$string = 'pakistan';
echo preg_match('/p.n/',$string);
The following regex:
$string = 'pakistan';
echo preg_match('~^p.+n$~',$string);
Anchors the string to the beginning and end and requires at least one (but unlimited times) character between p and n.
Use below code:-
preg_match("/p.*n/U", $string, $match);
echo '<pre>'; print_r($match[0]);
If you want to count how many times specific word is repeating in the string
Use preg_match_all
preg_match_all ("/p.*n/U", $string, $match);
echo '<pre>'; print_r($match[0]);
Hope it will help you :)

Regex to return contents of word and bracket

What Regex pattern would I need to extract the contents of a pair of parentheses in PHP which have a preceding specific string?
So if I have a statement
#includelayout( 'cms.layout.nav-header' )
I only need the contents of the parentheses that are directly preceded by #includelayout.
So I just want it to return:
'cms.layout.nav-header'
I am currently using:
preg_match('/(?<!\w)(?:\s*)#includelayout((?:\s*)?\(.*)/', $value, $matches);
which gives me
array (size=2)
0 => string '#includelayout( 'output.layout.nav-header' )' (length=46)
1 => string '( 'output.layout.nav-header' )' (length=30)
but I just can't get it to not return the parentheses.
Thanks
Get the matched group from index 1:
(?<=#includelayout\()([^)]*)
DEMO
Sample code:
$re = "/(?<=#includelayout\\()([^)]*)/i";
$str = "#includelayout( 'cms.layout.nav-header' )";
preg_match_all($re, $str, $matches);
You could try the below regex to match the contents within paranthesis without leading and the following spaces inside paranthesis,
#includelayout\(\s*\K.*?(?=\s*\))
DEMO
If you want to match all the characters which are enclosed within () preceeded by the string #includelayout then you could try the below.
#includelayout\(\K[^)]*
DEMO
Your PHP code would be,
<?php
$mystring = "#includelayout( 'cms.layout.nav-header' )";
$regex = '~#includelayout\(\K[^)]*~';
if (preg_match($regex, $mystring, $m)) {
$yourmatch = $m[0];
echo $yourmatch;
}
?> //=> 'cms.layout.nav-header'

Split string at the first non-alphabetic character

How can I get a portion of the string from the beginning until the first non-alphabetic character?
Example strings:
Hello World
Hello&World
Hello5World
I'd like to get the "Hello" part
You need to use the preg_split feature.
$str = 'Hello&World';
$words = preg_split('/[^\w]/',$str);
echo $words[0];
You can access Hello by $words[0], and World by $words[1]
You can use preg_match() for this:
if (preg_match('/^([\w]+)/i', $string, $match)) {
echo "The matched word is {$match[1]}.";
}
Change [\w]+ to [a-z]+ if you do not want to match the 5 or any numeric characters.
Use preg_split. Split string by a regular expression
If you only want the first part, use preg_match:
preg_match('/^[a-z]+/i', $str, $matches);
echo $matches[0];
Here's a demo.
Use preg_split to get first part of alpha.
$array = preg_split('/[^[:alpha:]]+/', 'Hello5World');
echo $array[0];

PHP Regular expression

Some links like these:
[links url='http://www.google.com.hk' title='Google' image='']google[/links]
[links url='http://hk.yahoo.com' title='yahoo' image='']yahoo[/links]
how to use PHP Regular expression get the url attributes? Thanks.
http://www.google.com.hk
http://hk.yahoo.com
This should get you started:
preg_match_all("/\[links[^\]]+url='([^']+)'/", '{{your data}}', $arr, PREG_PATTERN_ORDER);
Explanation of the regex:
/
\[ //An excaped "[" to make it literal.
links //The work "links"
[^\]]+ //1+ non-closing brackent chars ([^] is a negative character class)
url=' //The work url='
([^']+) //The contents inside the '' in a caputuring group
/
Use this regex: /\[links\s+(?:[^\]]*\s+)*url=\'([^\']*)\'[^\]]*?\]/
$str = "[links url='http://www.google.com.hk' title='Google' image='']google[/links]";
$m = array();
preg_match('/\[links\s+(?:[^\]]*\s+)*url=\'([^\']*)\'[^\]]*?\]/', $str, $m);
echo $m[1];

what is the regular expression for this

I want to parse this
(adv) much (thanks)
I want to eliminate the words and the bracket (adv) but not (thanks)
the condition is:
inside bracket, and word length inside bracket is 1-5 characters
I am using preg_match in PHP
$matches = NULL;
preg_match("/\([^\)]{1,5}\)/", "(adv) much (thanks)", $matches);
var_export($matches);
array (
0 => '(adv)',
)
$str = '(adv) much (thanks)';
$str = preg_replace('/\(\w{1,5}\) ?/', '', $str);

Categories