Adding custom masks to phone numbers - php

So i'm creating a simple function to mask phone numbers. My phone numbers have a 9 digits and i want preg_replace them with a given mask like 2-2-2-1-2 or 3-2-2-2 and etc.
I tried this:
$mask = explode('-', '3-2-2-2');
$pattern = '';
$replace = '';
foreach ($mask as $key => $value) {
if ($key == 0) {
$pattern = '/\(?(\d{' . $value . '})\)?[- ]';
$replace = '$' . ++$key . '-';
continue;
}
if ($key == count($mask) - 1) {
$pattern .= '?(\d{' . $value . '})/';
$replace .= '$' . ++$key;
break;
}
$pattern .= '?(\d{' . $value . '})[- ]';
$replace .= '$' . ++$key . '-';
}
return preg_replace($pattern, $replace, '902000810');
and the result is 902-00-08-10. Sometimes getting error preg_replace(): No ending delimiter '/' found. How can i refactor this to not getting errors?

Assuming:
$num = '902000810';
$mask = explode('-', '3-2-2-2');
There're other ways than using regex to format a phone number from the mask.
using formatted strings:
$maskPH = array_map(fn($i) => "%{$i}s", $mask);
$formatI = implode('', $maskPH);
$formatO = implode('-', $maskPH);
$result = vsprintf($formatO, sscanf($num, $formatI));
using unpack:
$format = array_reduce($mask, function ($c, $i) {
static $j = 0;
return "{$c}A{$i}_" . $j++ . "/";
});
$result = implode('-', unpack($format, $num));

preg_replace(): No ending delimiter '/' found
means that your pattern does not terminate with a / as last character.
But all three patterns lack proper formatting:
You should modify them accordingly.
From:
$pattern = '/\(?(\d{' . $value . '})\)?[- ]';
$pattern .= '?(\d{' . $value . '})/';
$pattern .= '?(\d{' . $value . '})[- ]';
To:
$pattern = '/\(?(\d{' . $value . '})\)?[- ]/';
$pattern .= '/?(\d{' . $value . '})/';
$pattern .= '/?(\d{' . $value . '})[- ]/';

Related

ignore url in PHP regex

I've got a utility where I'm trying to enforce brand standards in an application where the function will wrap brand words in a span with a class.
public function filterBrandWords($text)
{
// look up the brand words from the config settings
$filter_terms = ['brandword1', 'brandword2', 'brandword3'];
$filtered_text = $text;
foreach ($filter_terms as $word) {
$match_count = preg_match_all('/' . $word . '/i', $text, $matches);
for ($i = 0; $i < $match_count; $i++) {
$brand_string = trim($matches[0][$i]);
$lower = strtolower($brand_string);
$new = '<span class="font-semibold">' . substr($lower, 0, 3) . '</span>' . substr($lower, 3);
$filtered_text = preg_replace('/\b' . $brand_string . '\b/', $new, $filtered_text);
}
}
return $filtered_text;
}
This works but noticed that it's also filtering text that contains the brand URL when applied.
I tried amending $match_count = preg_match_all('/' . $word . '/i', $text, $matches); to $match_count = preg_match_all('/' . $word . 'com$' . '/i', $text, $matches); in the hope it would ignore matches with com in them.
What have I gotten wrong here the regex?
If I do
echo filterBrandWords('brandword1');
the output is
<span class="font-semibold">bra</span>ndword1
with a URL, the output is
<span class="font-semibold">bra</span>ndword1.com
In those instances, I want to ignore the filter and just give it straight.
If you want to ignore anything like a URL you can use something like this as your regex:
(?|.*\.(com|net|org))
which is a Negative Lookahead assertion that matches URL's (broadly). Insert that into your function as I have done here:
function filterBrandWords($text)
{
// look up the brand words from the config settings
$filter_terms = ['brandword1', 'brandword2', 'brandword3'];
$filtered_text = $text;
if(!preg_match('/(?|.*\.(com|net|org))/', $filtered_text)) { // if it resembles a URL, skip it
foreach ($filter_terms as $word) {
$match_count = preg_match_all('/' . $word . '/i', $text, $matches);
for ($i = 0; $i < $match_count; $i++) {
$brand_string = trim($matches[0][$i]);
$lower = strtolower($brand_string);
$new = '<span class="font-semibold">' . substr($lower, 0, 3) . '</span>' . substr($lower, 3);
$filtered_text = preg_replace('/\b' . $brand_string . '\b/', $new, $filtered_text);
}
}
}
return $filtered_text;
}
Now call the function with something resembling a URL:
echo filterBrandWords('brandword1.com');
And the entire URL is just returned:
brandword1.com
EXAMPLE

Error Array to String JPG Error

I want to fetch a string to find the URLs, store them in an array and use them to build an custome output. The problem is that I get an error that I have a string to array conversion.
NOTICE Array to string conversion on line number 21 JPEG (Array)
Can some help me, is there another way to get the URL which is stored in the array?
function getURLSfromStringToImages($string) {
$regex = '/https?\:\/\/[^" ]+/i';
preg_match_all($regex, $string, $matches);
//($matches[0]);
$tmp = '';
$tmpArray = $matches;
if ( is_array( $tmpArray ) ) {
$size = count( $tmpArray ); //array size by using count() function
$index = 0;
while ( $index < $size ) {
$tmp .= '<anhang location="REMOTE" gruppe="BILD">' . "\n"
. '<format>JPEG</format>' . "\n"
. '<daten>' . "\n"
. '<pfad>' . "($tmpArray[$index])" . '</pfad>' . "\n"
. '</daten>' . "\n"
. '</anhang>' . "\n \n";
$index ++;
}
}
echo $tmp;
}
$urls = getURLSfromStringToImages('a:3:{i:0;a:9:{s:22:"real_estate_floor_name";s:7:"Karnten";s:23:"real_estate_floor_price";s:0:"";s:31:"real_estate_floor_price_postfix";s:0:"";s:22:"real_estate_floor_size";s:0:"";s:30:"real_estate_floor_size_postfix";s:0:"";s:26:"real_estate_floor_bedrooms";s:0:"";s:27:"real_estate_floor_bathrooms";s:0:"";s:29:"real_estate_floor_description";s:0:"";s:23:"real_estate_floor_image";a:2:{s:2:"id";s:4:"4243";s:3:"url";s:70:"https://www.immo-verteiler.de/wp-content/uploads/2018/06/grundriss.jpg";}}i:1;a:9:{s:22:"real_estate_floor_name";s:6:"sterer";s:23:"real_estate_floor_price";s:0:"";s:31:"real_estate_floor_price_postfix";s:0:"";s:22:"real_estate_floor_size";s:0:"";s:30:"real_estate_floor_size_postfix";s:0:"";s:26:"real_estate_floor_bedrooms";s:0:"";s:27:"real_estate_floor_bathrooms";s:0:"";s:29:"real_estate_floor_description";s:0:"";s:23:"real_estate_floor_image";a:2:{s:2:"id";s:4:"4211";s:3:"url";s:76:"https://www.immo-verteiler.de/wp-content/uploads/2018/06/Hotel-Goestling.jpg";}}i:2;a:9:{s:22:"real_estate_floor_name";s:13:"lkjölkjölkj";s:23:"real_estate_floor_price";s:0:"";s:31:"real_estate_floor_price_postfix";s:0:"";s:22:"real_estate_floor_size";s:0:"";s:30:"real_estate_floor_size_postfix";s:0:"";s:26:"real_estate_floor_bedrooms";s:0:"";s:27:"real_estate_floor_bathrooms";s:0:"";s:29:"real_estate_floor_description";s:0:"";s:23:"real_estate_floor_image";a:2:{s:2:"id";s:4:"3907";s:3:"url";s:66:"https://www.immo-verteiler.de/wp-content/uploads/2018/05/klein.jpg";}}}');
print_r($urls);
You need two loops
while ( $index < $size ) {
foreach($tmpArray[$index] as $path)
$tmp .= '<anhang location="REMOTE" gruppe="BILD">' . "\n"
. '<format>JPEG</format>' . "\n"
. '<daten>' . "\n"
. '<pfad>' . $path . '</pfad>' . "\n"
. '</daten>' . "\n"
. '</anhang>' . "\n \n";
$index ++;
}
also, I suggest you use the heredoc syntax: https://www.php-space.info/php/space/heredoc-syntax.php
Matches are obtained into an array of arrays. You need to only grab full match values, that are stored in $matches[0].
Hence, use
$tmpArray = $matches[0]; // not $tmpArray = $matches;
Also, just use $tmpArray[$index] instead of "($tmpArray[$index])".
See the PHP demo.

var_export prettifier / visualizer

I'm using var_export to dump output to logs when errors occur. However since the result is in pure text, I don't get a chance to push it through some sort of library like krumo so I can interactively explores the output.
What methods do people have to deal with making var_export text more readable?
Here is my function, it works well for multidimensional arrays:
function VE($varname, $varval, $short_syntax=true, $tag = ' ', $comma='', $end_line="\r\n") {
$res = '';
if($short_syntax){
$begin_array = '[';
$end_array = ']';
} else {
$begin_array = 'array(';
$end_array = ')';
}
$arr = explode('/',$varname);
$dim =count($arr)-1;
$lastKey = end($arr);
if (! is_array($varval)){
if( is_string($varval)) $varval = "'$varval'";
$res .= str_repeat($tag,$dim) . $lastKey . ' => ' . $varval . $comma . $end_line;
}else{
$res .= str_repeat($tag,$dim) . $lastKey . ' => ' . $begin_array . $end_line;
$count_varval = 0;
$dim_varval = count($varval);
foreach ($varval as $key => $val){
$count_varval++;
if($count_varval<$dim_varval) $commma=','; else $commma='';
if( is_string($key)) $key = "'$key'";
$res .= VE ($varname . "/" . $key , $val, $short_syntax, $tag, $commma);
}
$res .= str_repeat($tag,$dim) . $end_array . $comma . $end_line;
}
return $res;
}
$bigarray = array(); // your array
$bb = VE ('$bigarray', $bigarray);
echo "<pre>$bb</pre>";
I hope it helps ;)

How to wrap words of string every 1000 characters in php

i have some big string, and some array of words that must be replaced with some changes, like wrapping in link. First issue is wrap whole words or combinations words. And the second issue is do previous step minimum every 1000 characters.
$string="lalala word lalala blah, blah lalala combination of words lalala lalala...";
$patterns=array('word','combination of words');
$replacements=array('word','combination of words');
For an example, what i must to do with snippet before?
It sounds to me like you're looking for wordwrap(). You can then use preg_replace_callback() to apply it to your search patterns and make the replacements:
foreach ($patterns as $pattern) {
$regex = '/' . preg_quote($pattern, '/') . '/';
$string = preg_replace_callback($regex, function($match) {
return '<a href="#">'
. wordwrap(htmlspecialchars($match), 1000, '<br />')
. '</a>';
}, $string);
}
SOLUTION:
<?php
function set_keys_by_words($content, $key, $words,$before,$after) {
$positions = array();
$string = '';
for ($i = 0; $i < count($words); $i++) {
$string = preg_replace('/\b' . $words[$i] . '\b/ui', $key . $words[$i], $content);
$position = mb_strpos($string, $key);
if ($position != '') {
$positions[(int) $position] = $words[$i];
}
}
ksort($positions);
$word = '';
$number = '';
$i = 0;
foreach ($positions as $k => $v) {
$i++;
if ($i == 1) {
$number = $k;
$word = $v;
}
}
if ((int) $number) {
$word_len = strlen($word);
$part_after = preg_replace('/\b' . $word . '\b/ui', $before . $word . $after, mb_substr($content, 0, $number + $word_len));
echo $part_after . mb_substr($content, $number + $word_len, 1000);
$content = mb_substr($content, $number + $word_len + 1000);
if ($content != '') {
set_keys_by_words($content, $key, $words);
}
} else if ($number == '' && $content != '') {
echo $content;
}
}
?>

Adding conditional formatting and punctuation to a set of variables

I often need to list items separated by comma, space or punctuation, addresses are a classic example (This is overkill for an address and is for the sake of an example!):
echo "L$level, $unit/$num $street, $suburb, $state $postcode, $country.";
//ouput: L2, 1/123 Cool St, Funky Town, ABC 2000, Australia.
As simple as it sounds, is there an easy way to "conditionally" add the custom separators between variables only if the variable exists? Is it necessary to check if each variable is set? So using the above, another address with less detail may output something like:
//L, / Cool St, , ABC , .
A slightly arduous way of checking would be to see if each variable is set and display the punctuation.
if($level){ echo "L$level, "; }
if($unit){ echo "$unit"; }
if($unit && $street){ echo "/"; }
if($street){ echo "$street, "; }
if($suburb){ echo "$suburb, "; }
//etc...
It would be good to have a function that could automatically do all the stripping/formatting etc:
somefunction("$unit/$num $street, $suburb, $state $postcode, $country.");
Another example is a simple csv list. I want to output x items separated by comma:
for($i=0; $i=<5; $i++;){ echo "$i,"; }
//output: 1,2,3,4,5,
In a loop for example, what's the best way of determining the last item of an array or the loop condition is met to not include a comma at the end of the list? One long way around this I've read of is to put a comma before an item, except the first entry something like:
$firstItem = true; //first item shouldn't have comma
for($i=0; $i=<5; $i++;){
if(!$firstItem){ echo ","; }
echo "$i";
$firstItem = false;
}
For your first example, you can use arrays in conjunction with a few of the array methods to get the desired result. For example:
echo join(', ', array_filter(array("L$level", join(' ', array_filter(array(join('/', array_filter(array($unit, $num))), $street))), $suburb, join(' ', array_filter(array($state, $postcode))), $country))) . '.';
This one-liner is quite complicated to read, so one can always wrap the array, array_filter and join calls into a separate method, and use that:
function merge($delimiter)
{
$args = func_get_args();
array_shift($args);
return join($delimiter, array_filter($args));
}
echo merge(', ', "L$level", merge(' ', merge('/', $unit, $num), $street), $suburb, merge(' ', $state, $postcode), $country) . '.';
You need the array_filter calls to remove the empty entries, otherwise the delimeters would still be printed out.
For your second example, add the items to an array, then use join to insert the delimeter:
$arr = array();
for($i=0; $i=<5; $i++)
{
$arr[] = $i;
}
echo(join(',', $arr));
While Phillip's answer addresses your question, I wanted to supplement it with the following blog post by Eric Lippert. Although his discussion is in c#, it applies to any programming language.
There's a simple solution to your second problem:
for($i=0; $i<=5; $i++)
$o .= "$i,";
echo chop($o, ',');
ok, take that! (but not too serious ^^)
<?php
function bothOrSingle($left, $infix, $right) {
return $left && $right ? $left . $infix . $right : ($left ? $left : ($right ? $right : null));
}
function leftOrNull($left, $postfix) {
return $left ? $left . $postfix : null;
}
function rightOrNull($prefix, $right) {
return $right ? $prefix . $right : null;
}
function joinargs() {
$args = func_get_args();
foreach ($args as $key => $arg)
if (!trim($arg))
unset($args[$key]);
$sep = array_shift($args);
return join($sep, $args);
}
$level = 2;
$unit = 1;
$num = 123;
$street = 'Cool St';
$suburb = 'Funky Town';
$state = 'ABC';
$postcode = 2000;
$country = 'Australia';
echo "\n" . '"' . joinargs(', ', rightOrNull('L', $level), bothOrSingle(bothOrSingle($unit, '/', $num), ' ', $street), bothOrSingle($state, ' ', $postcode), bothOrSingle($country, '', '.')) . '"';
// -> "L2, 1/123 Cool St, ABC 2000, Australia."
$level = '';
$unit = '';
$num = '';
$street = 'Cool St';
$suburb = '';
$state = 'ABC';
$postcode = '';
$country = '';
echo "\n" . '"' . joinargs(
', ',
leftOrNull(
joinargs(', ',
rightOrNull('L', $level),
bothOrSingle(bothOrSingle($unit, '/', $num), ' ', $street),
bothOrSingle($state, ' ', $postcode),
$country
),
'.'
)
) . '"';
// -> "Cool St, ABC."
$level = '';
$unit = '';
$num = '';
$street = '';
$suburb = '';
$state = '';
$postcode = '';
$country = '';
echo "\n" . '"' . joinargs(
', ',
leftOrNull(
joinargs(', ',
rightOrNull('L', $level),
bothOrSingle(bothOrSingle($unit, '/', $num), ' ', $street),
bothOrSingle($state, ' ', $postcode),
$country
),
'.'
)
) . '"';
// -> "" (even without the dot!)
?>
yes, i know - looks a bit like brainfuck.
Philip's solution is probably best when working with arrays (if you don't have to filter out empty values), but if you can't use the array functions--for instance, when dealing with query results returned from mysqli_fetch_object()--then one solution is just a simple if statement:
$list = '';
$row=mysqli_fetch_object($result);
do {
$list .= (empty($list) ? $row->col : ", {$row->col}");
} while ($row=mysqli_fetch_object($result));
Or, alternatively:
do {
if (isset($list)) {
$list .= ", {$row->col}";
} else $list = $row->col;
} while ($row=mysqli_fetch_object($result));
To build a list and filter out empty values, I would write a custom function:
function makeList() {
$args = array_filter(func_get_args()); // as per Jon Benedicto's answer
foreach ($args as $item) {
if (isset($list)) {
$list .= ", $item";
} else {
$list = $item;
}
}
if (isset($list)) {
return $list;
} else return '';
}
Then you can call it like so:
$unitnum = implode('/',array_filter(array($unit,$num)));
if ($unitnum || $street) {
$streetaddress = trim("$unitnum $street");
} else $streetaddress = '';
if ($level) {
$level = "L$level";
}
echo makeList($level, $streetaddress, $suburb, $state $postcode, $country).'.';
I always find that its both faster and easier to use the language's array methods. For instance, in PHP:
<?php
echo join(',', array('L'.$level, $unit.'/'.$num,
$street, $suburb, $state, $postcode, $country));
Just take out the last comma, i.e replace it with nothing.
$string1 = "L$level, $unit/$num $street, $suburb, $state $postcode, $country.";
$string1 = eregi_replace(", \.$", "\.", $string1);
echo $string1;
This will do the work.
<?php
$level = 'foo';
$street = 'bar';
$num = 'num';
$unit = '';
// #1: unreadable and unelegant, with arrays
$values = array();
$values[] = $level ? 'L' . $level : null;
// not very readable ...
$values[] = $unit && $num ? $unit . '/' . $num : ($unit ? $unit : ($num ? $num : null));
$values[] = $street ? $street : null;
echo join(',', $values);
// #2: or, even more unreadable and unelegant, with string concenation
echo trim(
($level ? 'L' . $level . ', ' : '') .
($unit && $num ? $unit . '/' . $num . ', ' : ($unit ? $unit . ', ' : ($num ? $num . ', ': '')) .
($street ? $street . ', ': '')), ' ,');
// #3: hey, i didn't even know that worked (roughly the same as #1):
echo join(', ', array(
$level ? 'L' . $level : null,
$unit && $num ? $unit . '/' . $num : ($unit ? $unit : ($num ? $num : null)),
$street ? $street : null
));
?>

Categories