regex match with in { } - php

I'm having a lot of issues with matching all items that are in brackets.
Here is some code, what I need the matches to do is output something like
Array ( [0] => username [1] => id )
some code to give you an idea
$ur = '/account/{username}/{id}';
if(preg_match('', $str, $matches)){
print_r($matches);
}
How can I accomplish this?

$ur = '/account/{username}/{id}';
preg_match_all('/\{([a-z]+)\}/',$ur,$m);
echo '<pre>';
var_dump($m[1]);
output:
array(2) {
[0]=>
string(8) "username"
[1]=>
string(2) "id"
}

Related

Phone Number Regex Conditions

I need to create a regex function to check phone numbers entered on a phone number field based on these conditions.
• if the number starts with either 6, 8 or 9 and another 7 digits
• or if the number starts with either +656, +658 or +659 and another 7 digits
So basically its singapore phone number with +65 being the country code
6, 8 or 9 are the only starting digit of phone numbers.
I have tried code below but it wont work.
add_filter( 'gform_phone_formats', 'sg_phone_format' );
function sg_phone_format( $phone_formats ) {
$phone_formats['sg'] = array(
'label' => 'Singapore',
'mask' => false,
'regex' => '/^[689]\d{7}$/D|/^(\+656)\d{7}$/D|/^(\+658)\d{7}$/D|/^(\+659)\d{7}$/D',
);
return $phone_formats;
}
Thank you!
I would use this regex pattern for Singapore numbers:
^(?:\+65)?[689][0-9]{7}$
Sample script:
$number = "+6587654321";
if (preg_match("/^(?:\+65)?[689][0-9]{7}$/", $number)) {
echo "MATCH";
}
So basically the set condition for a regex match can be found by alternating a grouping of sets that match the occurrences of the desired character or number, like so:
<code>
//$regex = '/\+65+[?:6?8:?:9]+[0-9]{3}+[0-9]{4}/'; //wrong
// Edited the following lines
$regex = '/(?:\+65[6|8|9]+[\d]{7})|(?:\+65[689][\-|\s|\/][0-9]{3}+[\-|\s|\/][0-9]{4})/';
$phone = '+6561234567,+6587654321,+659-432-1567,+6594321567,+659 765 4321,+658/765/1234,+659--432--1567,+6555?:?1231234'; //test string
//Match $var against a regular expression
preg_match_all($regex, $phone, $matches, PREG_SET_ORDER, 0);
//var_dump
var_dump($matches) . " \n";
//print_r
print_r ($matches) . " \n";
//echo single value array();
echo $matches[0][0];
echo "<br />";
echo $matches[1][0];
echo "<br />";
echo $matches[2][0];
echo "<br />";
echo $matches[3][0];
echo "<br />";
echo $matches[4][0];
echo "<br />";
echo $matches[5][0];
echo "<br />";
echo $matches[6][0];
</code>
The result from the above code is as follow:
<pre>
//var_dump
array(6) { [0]=> array(1) { [0]=> string(11) "+6561234567" } [1]=> array(1) { [0]=> string(11) "+6587654321" } [2]=> array(1) { [0]=> string(13) "+659-432-1567" } [3]=> array(1) { [0]=> string(11) "+6594321567" } [4]=> array(1) { [0]=> string(13) "+659 765 4321" } [5]=> array(1) { [0]=> string(13) "+658/765/1234" } }
//print_r
Array ( [0] => Array ( [0] => +6561234567 ) [1] => Array ( [0] => +6587654321 ) [2] => Array ( [0] => +659-432-1567 ) [3] => Array ( [0] => +6594321567 ) [4] => Array ( [0] => +659 765 4321 ) [5] => Array ( [0] => +658/765/1234 ) )
//echo single value array();
+6561234567
+6587654321
+659-432-1567
+6594321567
+659 765 4321
+658/765/1234
</pre>
You can easily match more personalized regular expressions by setting specific conditions to match the string by escaping additional characters with
\ or any whitespace character with
\s
<code>
// add more characters to regex to match +659-432-1567
$regex = '/\+65[6|8|9]+[\-][0-9]{3}+[\-][0-9]{4}/';
</code>
The result from the modified regular expression will be
<pre>
array(1) { [0]=> array(1) { [0]=> string(13) "+659-432-1567" } }
Array ( [0] => Array ( [0] => +659-432-1567 ) )
+659-432-1567 //echo one match out of six
</pre>

PHP Why two same value from two different string are not equal to each other

Why are these two strings not equal?
I tried to get the same name so I can create a file, however I cannot get two strings equal to each other, even though I think both strings have the same value.
I uploaded var_dump output
any idea how to fix it?
$selectCategory = scandir($_SERVER['DOCUMENT_ROOT'].'/database/');
$cat = explode('.',$category);
print_r($cat);
print_r($selectCategory);
if($cat[0] == $selectCategory[2]){
echo " true";
}
else{
echo "no";
}
output:
Array ( [0] => bus [1] => php )
Array ( [0] => . [1] => .. [2] => bus [3] => fruit )
no
This is var_dump output
array(2) { [0]=> string(5) " bus" [1]=> string(3) "php" }
array(4) { [0]=> string(1) "." [1]=> string(2) ".." [2]=> string(3) "bus" [3]=> string(5) "fruit" }
no
As you can see from the var_dump output the items you are comparing are different lengths. There is a space and possibly a hidden character in the $cat one:
To trim all space and some other characters use this:
$cat = array_map('trim', $cat);
$selectCategory = array_map('trim', $selectCategory);

Separate items in a string into two arrays

How can I split this string into two arrays in PHP? The string may have many items in it.
$str = "20x9999,24x65,40x5";
I need to get two arrays from this string:
$array1 = array(20,24,40);
$array2 = array(9999,65,5);
I've tried many implementations of preg_split, slice, regex. I can't get it done... I need help!
You can explode the string by commas, and then explode each of those values by x, inserting the result values from that into the two arrays:
$str = "20x9999,24x65,40x5";
$array1 = array();
$array2 = array();
foreach (explode(',', $str) as $xy) {
list($x, $y) = explode('x', $xy);
$array1[] = $x;
$array2[] = $y;
}
Alternatively, you can use preg_match_all, matching against the digits either side of the x:
preg_match_all('/(\d+)x(\d+)/', $str, $matches);
$array1 = $matches[1];
$array2 = $matches[2];
In both cases the output is:
Array
(
[0] => 20
[1] => 24
[2] => 40
)
Array
(
[0] => 9999
[1] => 65
[2] => 5
)
Demo on 3v4l.org
I guess with preg_split, we could do so:
$str = '20x9999,24x65,40x5';
$array1 = $array2 = array();
foreach (preg_split("/,/", $str, -1, PREG_SPLIT_NO_EMPTY) as $key => $value) {
$val = preg_split("/x/", $value, -1, PREG_SPLIT_NO_EMPTY);
array_push($array1, $val[0]);
array_push($array2, $val[1]);
}
var_dump($array1);
var_dump($array2);
Output
array(3) {
[0]=>
string(2) "20"
[1]=>
string(2) "24"
[2]=>
string(2) "40"
}
array(3) {
[0]=>
string(4) "9999"
[1]=>
string(2) "65"
[2]=>
string(1) "5"
}

PHP - Splitting string lines up into two variables

Not sure how I would do this but if someone could point me in the right track that'll be great, basically I've got a lone line of text in a variable which looks like this:
Lambo 1; Trabant 2; Car 3;
Then I want to split "Lambo" to it's own variable then "1" to it's own variable, and repeat for the others. How would I go and do this?
I know about explode() but not sure how I would do it to split the variable up twice etc.
As requested in the comments my desired output would be like this:
$Item = "Lambo"
$Quantity = 1
Then echo them out and go back to top of loop for example and do the same for the Trabant and Car
<?php
$in = "Lambo 1; Trabant 2; Car 3;";
foreach (explode(";", $in) as $element) {
$element = trim($element);
if (strpos($element, " ") !== false ) {
list($car, $number) = explode(" ", $element);
echo "car: $car, number: $number";
}
}
You can use explode to split the input on each ;, loop over the results and then split over each .
You can use preg_split and iterate over the array by moving twice.
$output = preg_split("/ (;|vs) /", $input);
You could use preg_match_all for getting those parts:
$line = "Lambo 1; Trabant 2; Car 3;";
preg_match_all("/[^ ;]+/", $line, $matches);
$matches = $matches[0];
With that sample data, the $matches array will look like this:
Array ( "Lambo", "1", "Trabant", "2", "Car", "3" )
$new_data="Lambo 1;Trabant 2;Car 3;" ;
$new_array=explode(";", $new_data);
foreach ($new_array as $key ) {
# code...
$final_data=explode(" ", $key);
if(isset($final_data[0])){ echo "<pre>".$final_data[0]."</pre>";}
if(isset($final_data[1])){echo "<pre>".$final_data[1]."</pre>";}
}
This places each word and number in a new key of the array if you need to acess them seperatly.
preg_match_all("/(\w+) (\d+);/", $input_lines, $output_array);
Click preg_match_all
http://www.phpliveregex.com/p/fM8
Use a global regular expression match:
<?php
$subject = 'Lambo 1; Trabant 2; Car 3;';
$pattern = '/((\w+)\s+(\d+);\s?)+/Uu';
preg_match_all($pattern, $subject, $tokens);
var_dump($tokens);
The output you get is:
array(4) {
[0] =>
array(3) {
[0] =>
string(8) "Lambo 1;"
[1] =>
string(10) "Trabant 2;"
[2] =>
string(6) "Car 3;"
}
[1] =>
array(3) {
[0] =>
string(8) "Lambo 1;"
[1] =>
string(10) "Trabant 2;"
[2] =>
string(6) "Car 3;"
}
[2] =>
array(3) {
[0] =>
string(5) "Lambo"
[1] =>
string(7) "Trabant"
[2] =>
string(3) "Car"
}
[3] =>
array(3) {
[0] =>
string(1) "1"
[1] =>
string(1) "2"
[2] =>
string(1) "3"
}
}
In there the elements 2 and 3 hold exactly the tokens you are looking for.

Split an underscore-delimited string, but ignore underscores occurring in a url

Below string I need to split. I tried with the explode(), but it is fragmenting the url substring.
$link = "7_5_7_http://test.com/folder/images/7_newim/5_car/7_february2013/p/a00/p01/video-1.mov_00:00:09";
$ex_link = explode('_',$link);
It is splitting the string after every "_' symbol, but I need to the results like this:
$ex_link[0] = '7';
$ex_link[1] = '5';
$ex_link[2] = '7';
$ex_link[3] = 'http://test.com/folder/images/7_newim/5_car/7_february2013/p/a00/p01/video-1.mov';
$ex_link[2] = '00:00:09';
Explode has a third parameter, why do people complicate things ?
$link = "7_5_7_http://test.com/folder/images/7_newim/5_car/7_february2013/p/a00/p01/video-1.mov_00:00:09";
$array = explode('_', $link, 4);
$temp = array_pop($array);
$array = array_merge($array, array_reverse(array_map('strrev', explode('_', strrev($temp), 2)))); // Now it has just become complexer (facepalm)
print_r($array);
Output:
Array
(
[0] => 7
[1] => 5
[2] => 7
[3] => http://test.com/folder/images/7_newim/5_car/7_february2013/p/a00/p01/video-1.mov
[4] => 00:00:09
)
Online demo
Use
preg_match('/(\d)_(\d)_(\d)_([\w:\.\/\/\-]+)_([\d]{2}:[\d]{2}:[\d]{2})/', $link, $matches);
And $matches:
array(6) {
[0]=>
string(95) "7_5_7_http://test.com/folder/images/7_newim/5_car/7_february2013/p/a00/p01/video-1.mov_00:00:09"
[1]=>
string(1) "7"
[2]=>
string(1) "5"
[3]=>
string(1) "7"
[4]=>
string(80) "http://test.com/folder/images/7_newim/5_car/7_february2013/p/a00/p01/video-1.mov"
[5]=>
string(8) "00:00:09"
}
This one is simplest one
$result = preg_split('%_(?=(\d|http://))%si', $subject);

Categories