This question already has answers here:
Is there a PHP function that can escape regex patterns before they are applied?
(2 answers)
RegEx for word boundary but still match if is preceded or followed by special chars
(1 answer)
Closed 4 years ago.
I am trying to highlight words in a text by using pregreplace. The words are within an array which I do feed into pregreplace. This used to work, but stopped maybe due to upgrading php in the past, now it returns nothing.
// Function highlights $words in $str
function highlight_words($str, $words) {
global $color;
if(is_array($words)) {
foreach($words as $k => $word) {
// $pattern[$k] = "~\b($word)\b~is";
$pattern[$k] = "/$word/";
$replace[$k] = '<span style="background: '.$color[2].';color:'.$color[4].';">\\1</span>';
}
}
else {
$pattern = "~\b($words)\b~is";
$replace = '<span style="background: '.$color[2].';color:'.$color[4].';">\\1</span>';
}
return preg_replace($pattern,$replace ,$str);
}
echo highlight_words($text, $words);
values of pattern:
Array
(
[0] => /$18.5mUSD/
[1] => /$8,000,000.00/
[2] => /(at)/
[3] => /+43 688 649 45702/
...
values of $replace:
Array
(
[0] => <span style="background: #FF6B02;color:#FFFFFF;">\1</span>
[1] => <span style="background: #FF6B02;color:#FFFFFF;">\1</span>
[2] => <span style="background: #FF6B02;color:#FFFFFF;">\1</span>
[3] => <span style="background: #FF6B02;color:#FFFFFF;">\1</span>
...
preg_replace simply returns nothing. Seems to be a problem with array, but the syntax looks ok. If I change preg_replace to replace some simply regex pattern it will do, but it does not work with the array.
How can this be fixed?
Related
This question already has answers here:
Numeric Values and Special charectors in str_word_count function
(4 answers)
Closed 5 years ago.
I have a string "34_56_67_78_97 34_56_67_78_97 23_45_56_67_89 34_56_77_88_96 45_56_66_78_88 56_67_67_78_90" but when I run the code below the array comes back as null. The numbers linked with hyphens are a numerical word from my point of view and I would like to know which numerical words appear in the string 2 or more times. In this case the answer would be the numerical word 34_56_67_78_97.
$string="34_56_67_78_97 34_56_67_78_97 23_45_56_67_89 34_56_77_88_96 45_56_66_78_88 56_67_67_78_90"
$words = str_word_count($string, 1);
$frequency = array_count_values($words);
$result = array_filter($frequency, function ($x) { return $x >= 13; });
Found a link to the question here at stackoverflow:
Numeric Values and Special charectors in str_word_count function
So my code should look like this to make it work:
$str="34_56_67_78_97 34_56_67_78_97 23_45_56_67_89 34_56_77_88_96 45_56_66_78_88 56_67_67_78_90";
$somearray = str_word_count($str, 1, '1234567890:#&_');
print_r($somearray);
Print out of code:
Array
(
[0] => 34_56_67_78_97
[1] => 34_56_67_78_97
[2] => 23_45_56_67_89
[3] => 34_56_77_88_96
[4] => 45_56_66_78_88
[5] => 56_67_67_78_90
)
This question already has answers here:
Remove all special characters from a string [duplicate]
(3 answers)
Closed 6 years ago.
I have some problems with removing numbers and special characters. I want to remove all numbers and special characters from the input. Here's my code :
$input = $_POST["input"];
function preprocessing($input){
$input = trim(strtolower($input));
$remove = '/[^a-zA-Z0-9]/s';
$result = preg_split($remove, $input, -1, PREG_SPLIT_NO_EMPTY);
for($i = 0; $i < count($resultl); $i++){
$result[$i] = trim($result[$i]);
}
return $result;
}
String example :
qwd qwd qwdqd123 13#$%^&*) ADDA ''''
Output :
Array ( [0] => qwd [1] => qwd [2] => qwdqd123 [3] => 13 [4] => adda )
The numbers still appear on my string. How to solve this ?
Thank you before.
Check this
function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
return preg_replace('/[^A-Za-z\-]/', '', $string); // Removes special chars.
}
This question already has answers here:
What do 'lazy' and 'greedy' mean in the context of regular expressions?
(13 answers)
Closed 8 years ago.
i'm trying to parse tags from a string like the following:
$string = "foo [cmd:tag1] bar [cmd:tag2] bla bla";
$pattern = "/\[cmd:(.+)\]/";
preg_match_all($pattern, $string, $matches, PREG_OFFSET_CAPTURE);
$rc = $matches[0];
foreach($rc as $tag)
{
print_r2($tag);
}
which will return:
Array
(
[0] => [cmd:tag1] bar [cmd:tag2]
[1] => 4
)
what is wrong in my syntax as i'm expecting the following result:
Array
(
[0] => [cmd:tag1]
[1] => [cmd:tag2]
)
thanks
\[cmd:(.+?)\]
or use
\[cmd:([^\]]*)\]
Make your quantifier * non greedy by putting ? ahead of it.
See demo.
https://regex101.com/r/fA6wE2/23
This question already has answers here:
php explode all characters [duplicate]
(4 answers)
Closed 8 years ago.
how i can explode string into an array. Acctually i want to translate english language into the braille. First thing i need to do is to get the character one by one from a string,then convert them by mathing the char from value in database and display the braille code using the pic. As example when user enter "abc ef", this will create value separately between each other.
Array
(
[0] => a
[1] => b
[2] => c
[3] =>
[4] => e
[5] => f
)
i tried to do but not get the desired output. This code separated entire string.
$papar = preg_split('/[\s]+/', $data);
print_r($papar);
I'm sorry for simple question,and if you all have an idea how i should do to translate it, feel free to help. :)
If you're using PHP5, str_split will do precisely what you're seeking to accomplish. It splits each character in a string – including spaces – into an individual element, then returns an array of those elements:
$array = str_split("abc ef");
print_r($array);
Outputs the following:
Array
(
[0] => a
[1] => b
[2] => c
[3] =>
[4] => e
[5] => f
)
UPDATE:
The PHP4 solution is to use preg_split. Pass a double forward slash as the matching pattern to delimit the split character-by-character. Then, set the PREG_SPLIT_NO_EMPTY flag to ensure that no empty pieces will be returned at the start and end of the string:
$array = preg_split('//', "abc ef", -1, PREG_SPLIT_NO_EMPTY); // Returns same array as above
PHP has a very simple method of getting the character at a specific index.
To do this, utilize the substring method and pass in the index you are looking for.
How I would approach your problem is
Ensure the string you are converting has at least a size of one
Determine how long the string is
int strlen ( string $string )
Loop over each character and do some operation depending on that character
With a string "abcdef"
$rest = substr("abcdef", -1); // returns "f"
See the substr page for a full example and more information
http://us2.php.net/substr
You can iterate the string itself.
$data = 'something string';
for($ctr = 0; $ctr < strlen($data); $ctr++)
{
if($data{$ctr}):
$kk = mysql_query("select braille_teks from braille where id_huruf = '$data{$ctr}'");
$jj = mysql_fetch_array($kk);
$gambar = $jj['braille_teks'];
?>
<img src="images/<?php echo $gambar;?>">
<?php
else:
echo " ";
}
This question already has answers here:
Matches text inside brackets with Regex in PHP
(5 answers)
Closed 10 years ago.
i'm sorry i've already asked for this but couldn't find a solution yet :(
here's my string: (as you can see it has linebreaks)
Webname: [webname]
Username: [username]
IP: [IP]
i need to read out the values inside the square brackets.
here's my code:
$pattern = '/\[(.|\n)+?\]/'; // i've used the same syntax for my asp projects, always worked
preg_match($pattern, $txt, $matches, PREG_OFFSET_CAPTURE);
echo "matches:".count($matches)."\n\n";
foreach ($matches as $match)
{
echo $match[0]."\n";
}
i'm getting only 2 matches: [webname] and e (???)
i'm fiddling with this for hours now and can't find out what's wrong ..
any ideas?
thanks
Looks more complicated than it has to be. Line breaks don't play any role here.
$pattern = '/\[(.+?)\]/';
preg_match_all($pattern, $txt, $matches);
print_r($matches);
gives
Array
(
[0] => Array
(
[0] => [webname]
[1] => [username]
[2] => [IP]
)
[1] => Array
(
[0] => webname
[1] => username
[2] => IP
)
)
So the values would be in $matches[1]. If you want the values including the brackets ($matches[0]), you can also omit the parenthesis in the pattern.
The first capture group, in your case there is only one, is in $matches[1]
Try something like
$pattern = '/\[([^\]]+)\]/';
and use preg_match_all() to get all matches.
Try this
$values = array();
foreach (explode("\n", $text) as $line) {
if (preg_match('/([^:]++):[^\s]*+(.*+)/', $line, $match)) {
$values[$match[1]] = $match[2];
}
}
var_dump($values);