Remove everything outside specific value in brackets in a string PHP - php

I am trying to find a solution on how to remove everything outside specific value in brackets, including the value in brackets.
This is what I mean. I have this string
$str = "[:de]Some german text[:en]Some English text[:]";
What I want to achieve to get the text between [:de]and[:en] and to remove everything else, so the result has to be
$str = "Some german text";
I guess it should be some preg_match or some regex solution, but all I found was, how to remove the text in between, but not to keep the text in between and remove everything else.
Any ideas are welcome.

I guess,
(?<=\[:de\]).*?(?=\[:en\])
might work OK here.
Test
$re = '/(?<=\[:de\]).*?(?=\[:en\])/s';
$str = '[:de]Some german text 1[:en]Some English text[:] [:de]Some german text 2[:en]Some English text[:]
[:de]Some german text 3[:en]Some English text[:]';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
var_dump($matches);
Output
array(3) {
[0]=>
array(1) {
[0]=>
string(18) "Some german text 1"
}
[1]=>
array(1) {
[0]=>
string(18) "Some german text 2"
}
[2]=>
array(1) {
[0]=>
string(18) "Some german text 3"
}
}
If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.
RegEx Circuit
jex.im visualizes regular expressions:

Thanks to Emma's answer this is what I came up as a solution.
$re = '/(?<=\[:de\]).*?(?=\[:en\])/s';
$result = preg_match($re, $str, $match);
$result = $match[0];

Related

What is the patern to search for any string which respect this format "CEC0000-0000"?

The zeros can be incremented but it must be of four digits, so it could be CEC0152-2005
Of course with a "-" between them.
I used www.txt2re.com to generate this patern but it didn't help me.
Maybe,
^[A-Z]{3}[0-9]{4}-[0-9]{4}$
or,
^CEC[0-9]{4}-[0-9]{4}$
might work fine.
Test
$re = '/^[A-Z]{3}[0-9]{4}-[0-9]{4}$/m';
$str = 'CEC0152-2005
CEC0152-2019
CEC0152-1999
CEC0152-19991';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
var_dump($matches);
Output
array(3) {
[0]=>
array(1) {
[0]=>
string(12) "CEC0152-2005"
}
[1]=>
array(1) {
[0]=>
string(12) "CEC0152-2019"
}
[2]=>
array(1) {
[0]=>
string(12) "CEC0152-1999"
}
}
If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.
RegEx Circuit
jex.im visualizes regular expressions:
If after the dash we'd have a four-digit year,
^[A-Z]{3}[0-9]{4}-[12][0-9]{3}$
^CEC[0-9]{4}-[12][0-9]{3}$
might also work fine, I guess.
Demo 2

PHP pregmatch textarea

Ok,so I have this form that contains textareas and I want to verify they dont contain any illegal characters.
Html:
<textarea minlength="100" required name="Description" maxlength="800">
</textarea>
Php:
if(!preg_match("/^[-\p{L}\p{N} #&()!*,.;'\/\\\\]+$/u",$_POST["Description"])){
//error
}
I have tried multiple completely legal texts but it returns false.
What am I missing?
I guess your expression works fine, you might want to remove the u flag:
if(!preg_match("/^[-\p{L}\p{N} #&()!*,.;'\/\\\\]+$/s",$_POST["Description"])){
//error
}
Or, you might be trying to do,
if(!preg_match("/^[^-\p{L}\p{N} #&()!*,.;'\/\\\\]+$/s",$_POST["Description"])){
//error
}
if you want to exclude things out.
Demo 2
Test
$re = '/^[-\p{L}\p{N} #&()!*,.;\'\/\\\\]+$/m';
$str = 'abcd
abcd\\\\\\\\\\\\
&*&??
abc?
';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
var_dump($matches);
Output
array(2) {
[0]=>
array(1) {
[0]=>
string(4) "abcd"
}
[1]=>
array(1) {
[0]=>
string(10) "abcd\\\\\\"
}
}
If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.

Text replacement from a string in php

How do I replace the square bracket and its contents in php.
For example I would like to output something like
// 'Hello World Today Hello Everybody Anybody'
when I echo $txt.
$txt = 'Hello World [this-echo] Hello Everybody [nobody]';
That's a simple search and replace for your defined markers.
$text = 'Hello World [this-echo] Hello Everybody [nobody]';
$markers=array('[this-echo]','[nobody]');
$replacements=array('Today','Anybody');
$text= str_replace($markers,$replacements,$text);
Output
Hello World Today Hello Everybody Anybody
Fiddle
When the content of the brackets is not defined somewhere you can get them with preg_match_all:
$string = 'This is a [test]';
preg_match_all('/\[([^\]*]+?)\]/', $string, $matches);
The regex will match everything between the opening bracket and the closing bracket and put the results in the matches array.
The matches array for the example:
Array(2) {
[0]=>
array(1) {
[0]=>
string(6) "[test]"
}
[1]=>
Array(1) {
[0]=>
string(4) "test"
}
}
Now you can replace [test] with something you want and have the content between the brackets available.
Doing it by Regex is one way to achieve your text replacement:
$txt = 'Hello World [this-echo] Hello Everybody [nobody]';
$replaced_text = preg_replace('/(\\[[^\\]]*\\])/i', '<TEXTTOREPLACE>', $txt);
echo $replaced_text;
Explanation of the regular expression (/regex/options):
With:
/(\[[^\]]*\])/i
you are matching everything from [, all chars except ] to the closing ] ignoring the case with the i at the end of the pattern.
Ignoring the case (uppercase, lowercase):
/regex/i

Php preg_match issue not working

I am trying to find a php preg_match that can match:
"2-20 to 2-25"
from this text:
user levels 2-20 to 2-25 not ready
I tried
preg_match("/([0-9]+) to ([0-9]+)/", $vars[1] , $matchesto);
but the result is:
"20 to 2"
Any help appreciated.
Your pattern is almost correct; just include the dashes and adjust the capture group:
([-0-9]+ to [-0-9]+)
Example:
https://regex101.com/r/eD6lQ2/1
Thats because [0-9]+ matches one or more numbers but won't match a hyphen (-).
Try this:
$pattern = '~([0-9]+-[0-9]+) to ([0-9]+-[0-9]+)~Ui';
preg_match($pattern, $vars[1] , $matchesto);
You can use "\d" to match the digits:
<?php
$str = 'user levels 2-20 to 2-25 not ready';
$matches = array();
preg_match('/(\d+-\d+) to (\d+-\d+)/', $str, $matches);
var_dump($matches);
Output:
array(3) {
[0]=>
string(12) "2-20 to 2-25"
[1]=>
string(4) "2-20"
[2]=>
string(4) "2-25"
}

Need Regexp help PHP

I have for example such string - "7-th Road" or "7th number some other words" or "Some word 8-th word".
I need to get the first occurrence of number and all other next symbols to first occurrence of space.
So for examples above i need such values "7-th", "7th", "8-th".
And then from these matches like "7-th" i need extract only numbers in other operations.
Thanks in advance!
Regex should be /(\d+)([^\d]+)\s/ and the numbers would resolve to $1 and the ending characters to $2
Sample Code:
$string = '7-th Road';
preg_match_all('/(\d+)([^\d]+)\s/', $string, $result, PREG_PATTERN_ORDER);
var_dump($result[1]);
array(1) {
[0]=> string(1) "7"
}
var_dump($result[2]);
array(1) {
[0]=> string(1) "-th"
}
Are you asking for something like this?
#(\d+)-?(?:st|nd|rd|th)#
Example
If you would like to get just nums from the text use it:
preg_match_all('/(\d+)[th|\-th]*?/','7-th", "7th", "8-th', $matches);
But if you would like to remove 'th' or other just do replacement:
preg_replace('/(\d+)[th|\-th]*?/','$1', 'some string')
Not sure about the last one...

Categories