php preg_match extract args - php

Does anyone know how to get the int from this preg_match? It's only showing me the URI.
$uri = "/user/view/2";
$pattern = "/user/view/[0-9]+";
if(preg_match('#^' . $pattern . '$#', $uri, $matched)) {
print_r($matched);
}
else {
echo "No match!";
}

You have no capture group in your pattern. Change:
$pattern = "/user/view/[0-9]+";
To:
$pattern = "/user/view/([0-9]+)";
And it will be in $matched[1].

You can use T-Regx and go
pattern('^/user/view/[0-9]+$')->match($uri)
->forFirst(function (Match $match) {
$int = $match->group(1);
print_r($int);
})
->orReturn("No match!");
And you don't have to delimiter it with #!

Related

Remove everything after a string pattern in php

I have a list of patterns: $allowedTLDS = array(".de", ".at", ".ch", ".org", ".com", ".eu");
Now I a have string like 30212622mail#myname.com and I want to cut everything after the .com away to get the plain mail address.
I've written this function so far:
function extract_correct_email_address($string){
$allowedTLDS = array(".de", ".at", ".ch", ".org", ".com", ".eu");
$foundMatch = false;
$foundTLD = "";
foreach ($allowedTLDS as $tld) {
if (strpos($string, $tld) !== FALSE) {
//found a match
$foundMatch = true;
$foundTLD = $tld;
break 1;
}
}
if($foundMatch){
$str = strtok( $string, $foundTLD).$foundTLD;
return $str;
}
return NULL;
}
Now I have for example these addresses where I get the output on the right side:
input: info#meq.dewww.meq.de
output: info#meq.de
expected: info#meq.de (this one is correct)
input: info#cool-name.de
output: info#co.de
expected: info#cool-name.de
input: something-cool123#nice.decool123.nice.de
expected: something-cool123#nice.de
I hope it is understandable what I want to archive. What am I doing something wrong in the function?
Do it with this nice regular expression - you just can add TLDs:
function extract_correct_email_address($string){
// pattern do match email addresses
$pattern = '/[a-z0-9_\-\+]+#[a-z0-9\-]+\.(de|at|ch|org|com|eu)(?:\.[a-z]{2})?/i';
preg_match($pattern, $string, $matches);
if(count($matches[0])<1) {
return null;
}
return $matches[0];
}
// example string
$string = 'fdsaf das D hansi#test.dewww.lol.net franz#gibts.atdasf dasf asd';
var_dump(extract_correct_email_address($string));
Result:
string(15) "hansi#test.de"
Of course you can extend this to use for extracting multiple emails. Instead of matches[0] check all matches instead.
You can try this. I'm only inspecting the part after the "#". There could be the case somebody ist named "miller.denis#bla.com", so that .de would also be parsed
public function parseMail(){
$string = "something-cool123#nice.decool123.nice.de";
$allowedTLDS = array(".de", ".at", ".ch", ".org", ".com", ".eu");
$mail = explode("#", $string);
foreach ($allowedTLDS as $tld) {
if (strpos($mail[1], $tld) !== FALSE) {
//found a match
$foundTLD = $tld;
dump($mail[1]);
$str = strtok($mail[1], $foundTLD);
return $mail[0].'#'.$str.$foundTLD;
}
}
return NULL;
}
Try using regexp instead:
preg_replace('/(\.(de|at|ch|org|com|eu)).*$/', '\1', $email);
Just add your $allowedTLDS inside the parenthesis.
Examples
$ php -r "echo preg_replace('/(\.(de|at|ch|org|com|eu)).*$/', '\1', 'info#meq.dewww.meq.de') . PHP_EOL;"
> info#meq.de
$ php -r "echo preg_replace('/(\.(de|at|ch|org|com|eu)).*$/', '\1', 'info#cool-name.de') . PHP_EOL;"
> info#cool-name.de
$ php -r "echo preg_replace('/(\.(de|at|ch|org|com|eu)).*$/', '\1', 'something-cool123#nice.decool123.nice.de ') . PHP_EOL;"
> something-cool123#nice.de

php extract a sub-string before and after a character from a string

need to extract an info from a string which strats at 'type-' and ends at '-id'
IDlocationTagID-type-area-id-492
here is the string, so I need to extract values : area and 492 from the string :
After 'type-' and before '-id' and after 'id-'
You can use the preg_match:
For example:
preg_match("/type-(.\w+)-id-(.\d+)/", $input_line, $output_array);
To check, you may need the service:
http://www.phpliveregex.com/
P.S. If the function preg_match will be too heavy, there is an alternative solution:
$str = 'IDlocationTagID-type-area-id-492';
$itr = new ArrayIterator(explode('-', $str));
foreach($itr as $key => $value) {
if($value === 'type') {
$itr->next();
var_dump($itr->current());
}
if($value === 'id') {
$itr->next();
var_dump($itr->current());
}
}
This is what you want using two explode.
$str = 'IDlocationTagID-type-area-id-492';
echo explode("-id", explode("type-", $str)[1])[0]; //area
echo trim(explode("-id", explode("type-", $str)[1])[1], '-'); //492
Little Simple ways.
echo explode("type-", explode("-id-", $str)[0])[1]; // area
echo explode("-id-", $str)[1]; // 492
Using Regular Expression:
preg_match("/type-(.*)-id-(.*)/", $str, $output_array);
print_r($output_array);
echo $area = $output_array[1]; // area
echo $fnt = $output_array[2]; // 492
You can use explode to get the values:
$a = "IDlocationTagID-type-area-id-492";
$data = explode("-",$a);
echo "Area ".$data[2]." Id ".$data[4];
$matches = null;
$returnValue = preg_match('/type-(.*?)-id/', $yourString, $matches);
echo($matches[1]);

Remove any kind of url from string in php?

I am having below data.
Example 1
From : http://de.example.ch/biz/barbar-vintage-z%C3%BCrich
I want: /biz/barbar-vintage-z%C3%BCrich
And also if there is
http://www.example.ch/biz/barbar-vintage-z%C3%BCrich
Then
I also want
/biz/barbar-vintage-z%C3%BCrich
If you want to do it via regex then you can use:
$s = 'http://de.example.ch/biz/barbar-vintage-z%C3%BCrich';
echo preg_replace('~^https?://[^/]+~', '', $s);
//=> /biz/barbar-vintage-z%C3%BCrich
Otherwise as the comments says parse_url function also let you have this value.
function getRelativePath($url)
{
$matches = array();
if (preg_match('#^(http://|https://)([^./]+\.)+[a-z]{2,3}(/.*)$#', $url, $matches) {
return $matches[3];
} else {
return false;
}
}
You could use preg_match or preg_match_all
preg_match('~^https?://[^/]+\K.+~', $data, $matches);
DEMO
Just try this:
<?php
$url = 'http://de.example.ch/biz/barbar-vintage-z%C3%BCrich';
echo preg_replace('~^https?://[^/]+~', '', $url);
?>

PHP - Check if string contains illegal chars in string

In JS you can do:
var chs = "[](){}";
var str = "hello[asd]}";
if (str.indexOf(chs) != -1) {
alert("The string can't contain the following characters: " + chs.split("").join(", "));
}
How can you do this in PHP (replacing alert with echo)?
I do not want to use a regex for the simplicity of what I think.
EDIT:
What I've tried:
<?php
$chs = /[\[\]\(\)\{\}]/;
$str = "hella[asd]}";
if (preg_match(chs, str)) {
echo ("The string can't contain the following characters: " . $chs);
}
?>
Which obviously doesn't work and idk how to do it without regex.
In php you should do this:
$string = "Sometring[inside]";
if(preg_match("/(?:\[|\]|\(|\)|\{|\})+/", $string) === FALSE)
{
echo "it does not contain.";
}
else
{
echo "it contains";
}
The regex says check to see any of the characters are inside the string. you can read more about it here:
http://en.wikipedia.org/wiki/Regular_expression
And about PHP preg_match() :
http://php.net/manual/en/function.preg-match.php
Update:
I have written an updated regex for this, which captures the letters inside:
$rule = "/(?:(?:\[([\s\da-zA-Z]+)\])|\{([\d\sa-zA-Z]+)\})|\(([\d\sa-zA-Z]+)\)+/"
$matches = array();
if(preg_match($rule, $string, $matches) === true)
{
echo "It contains: " . $matches[0];
}
It returnes something like this:
It contains: [inside]
I have changed the regex only which becomes:
$rule = "/(?:(?:(\[)(?:[\s\da-zA-Z]+)(\]))|(\{)(?:[\d\sa-zA-Z]+)(\}))|(\()(?:[\d\sa-zA-Z]+)(\))+/";
// it returns an array of occurred illegal characters
It now returns [] for this "I am [good]"
Why not you try str_replace.
<?php
$search = array('[',']','{','}','(',')');
$replace = array('');
$content = 'hella[asd]}';
echo str_replace($search, $replace, $content);
//Output => hellaasd
?>
Instead of regex we can use string replace for this case.
here is a simple solution without using regex:
$chs = array("[", "]", "(", ")", "{", "}");
$string = "hello[asd]}";
$err = array();
foreach($chs AS $key => $val)
{
if(strpos($string, $val) !== false) $err[]= $val;
}
if(count($err) > 0)
{
echo "The string can't contain the following characters: " . implode(", ", $err);
}

PHP preg match?

Okay, let's say this is a line
v1=something;v2=something2;
how to get v1 value (something) starting from = and break at ; and same to be done with v2 by calling it (v1)
function getVal($name){
// some code to start grabbing from = and end by ;
}
when i call
getVal("v1");
it should return "something"
This will work
v1=([^;]*)
The match will be in group 1
Just replace v1 in the regex with the key you want to lookup
if (preg_match('/v1=([^;]*)/', $subject, $regs)) {
$result = $regs[1];
} else {
$result = "";
}
If I understand your question, then I think this is what you are looking for:
$line = "v1=something;v2=something2;";
function getVal($name, $line){
preg_match('/'.$name.'=([^;]*)/', $line, $matches);
return $matches[1];
}
echo getVal("v1", $line);
v(?:\d*)=(\w;+)
That will match all v (with digits or no digits after it) and then the match group will be after the = sign. It is group 1.
You are obliged to sent the line to your function (or you can be dirty and use it as global).
So, your function can be something like that :
<?php
function getVal($name, $line){
// some code to start grabbing from = and end by ;
preg_match('#;?' . $name . '=([^;]+);?#', $line, $aMatches);
if(isset($aMatches[1])) {
return $aMatches[1];
}
return false;
}
$line = 'v1=something;v2=something2';
$v1 = getVal('v1',$line);
echo $v1;
?>
Use this Function:
function getVal($name, $line){
preg_match("/{$name}=(.+);(v(\d+)=|$)/U", $line, $matches);
$matches = $matches[0];
$matches = preg_replace("/{$name}=/","",$matches);
$matches = preg_replace("/;v(\d+)=/","",$matches);
return $matches;
}
this will give you exact answer.
Tested and working.:)

Categories