Still having RegEx issues.. Need to match the following characters
a-zA-z9-0 , . ' " ( ) _ - : (SPACE)
Not all the values will have all these but could have them. I have everything withing but the parentheses, Single and Double Quoytes
/^[\w. ,\/:_-]+$/
UPDATE:
I got it working with this: "/^[\w. ,:()'\"-]+$/"
$val_1 = "Abh acb 123 . - _ 's ";
$val_2 = "Asc";
$val_3 = "234";
$val_4 = "nj%"; // Fail
$val_5 = "Help (me)";
$val_6 = "What's wrong?"; // Fail
$val_7 = "She's here";
$val_8 = "No: 123.00, 432.00";
$val_9 = 'Need to " Double" ';
$var_array = array($val_1, $val_2, $val_3, $val_4, $val_5, $val_6, $val_7, $val_8, $val_9);
foreach ($var_array as $k=>$d) {
if ((preg_match("/^[\w. ,:()'\"-]+$/", $d))) {
echo "Yeah it matches!!!<span style='color:green'>".$d."</span><br />";
} else {
echo "Try again, thie FAILED<span style='color:red'>".$d."</span><br />";
}
}
Thanks for all for helping out
$pat = "/^[\w. ,\\/:_()'\"-]/";
To match all those, you just need:
preg_match("/[a-zA-Z0-9,.'\"()_- :]/", $string);
/^[-a-zA-Z0-9,.'"()_: ]+$/
This should work. But if you put it into a string be sure to escape the needed quotes.
With the help of the other submitting I have found the solution that works:
"/^[\w. ,:()'\"-]+$/"
Thanks to all for the help
Related
I have a regex code which finds all URLs and replaces them with a HTML link. Here is my code:
// initializing
$str = "this is a good website www.example.com/classname/methodname/arg";
$rexProtocol = '(https?://)?';
$rexDomain = '((?:[-a-zA-Z0-9]{1,63}\.)+[-a-zA-Z0-9]{2,63}|(?:[0-9]{1,3}\.){3}[0-9]{1,3})';
$rexPort = '(:[0-9]{1,5})?';
$rexPath = '(/[!$-/0-9:;=#_\':;!a-zA-Z\x7f-\xff]*?)?';
$rexQuery = '(\?[!$-/0-9:;=#_\':;!a-zA-Z\x7f-\xff]+?)?';
$rexFragment = '(#[!$-/0-9:;=#_\':;!a-zA-Z\x7f-\xff]+?)?';
function callback($match){
// Prepend http:// if no protocol specified
$completeUrl = $match[1] ? $match[0] : "http://{$match[0]}";
$DetectProperName = strlen($match[2].$match[3].$match[4]) > 20 ? "...".substr($match[2].$match[3].$match[4],0,20) : $match[2].$match[3].$match[4];
return ''.$DetectProperName. '';
}
echo $str = preg_replace_callback("&\\b$rexProtocol$rexDomain$rexPort$rexPath$rexQuery$rexFragment(?=[?.!,;:\"]?(\s|$))&",'callback', htmlspecialchars($str));
Also here is the output:
this is a good website ...www.example.com/clas
Also here is a fiddle
Well, that's ok and it works as well for links. Now my question is about when input is containing a quote ' or ". That regex will add a \ next to it. How can I fix it? I want such a regex be not sensitive to quotes.
Here is an example:
Input:
$str = 'this is a " (quote)';
Current Output:
this is a \" (quote)
What I want:
this is a " (quote)
How can I do that?
Edit: According to some tests, I figured out that change single/double quotes to ASKII code. How can I prevent it?
I have a form where people enter their multiple codes.
Now, I would like to delete the spaces between these codes.
However, my code doesnt seem to work. Any ideas?
$codes = $_GET['codes'];
$spaces = strpos($codes, " ");
for($spaces; $spaces=0; $spaces--){
str_replace(" ", "", $codes);
echo $codes;
}
EDIT: I just have tried something else but it still doesnt work at all. I mean the echo gives me the original string every single time.
$codes = $_GET['codes'];
$cleancodes = str_replace(" ", "", $codes);
$cleancodes = trim(preg_replace('/\s\s+/', ' ', $cleancodes));
echo "<br / >" . $cleancodes;
$string = str_replace(' ', '', $string);
$text=str_replace(" ","",$text);
But doing that for code? Bound to break (if you meant program code)!
Use str_replace():-
<?php
$_GET['codes'] = "abc def ghi jkl mno pqr stu vwx yz ";
$codes = $_GET['codes'];
$codes = str_replace(" ","",$codes);
echo $codes;
Output:-https://eval.in/395364
i need to remove whitespaces from the beginning and end of loop phrase
All words come from an loop, and look like this: " Hello all people "
I'm using the code -
$appliedFilters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
foreach ($appliedFilters as $item) {
if ($item->getFilter()->getRequestVar() == 'b_car' || 'c_model' || 'd_year') {
$n_str = string.replace("\"", "", $item->getLabel()));
echo $n_str;
}
}
This code returns "Helloallpeople"
But i need "Hello all people"
Please help!
UPDATED
var_dump($item->getLabel()); returns string(7) "Hello " string(8) "all " string(5) "People "
Try Regx like the following:
$returnValue = preg_replace("/>\s+(.*)\s+</", '>$1<', '<a> Hello all people <a/>');
I am keeping the old answer for reference.
But if you simply want to remove leading and trailing spaces; use trim()
Update:
If you want to trim each and every element of an array; you can map trim function to it.
Then you can also implode the array to a string.
<?php
$str = array(" Hello all ", " Hello all people ", " all people ", " Hello people ");
$n_str = array_map("trim",$str);
var_dump($n_str);
echo implode(" ",$n_str);
?>
Update 2:
Ok I got it; It's not an array. It's a loop.
Every time $item->getLabel() returns just a string. it's not an array.
Following should help you.
$appliedFilters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
$result = "";
foreach ($appliedFilters as $item) {
if ($item->getFilter()->getRequestVar() == 'b_car' || 'c_model' || 'd_year') {
$result .= " ".trim($item->getLabel());
}
}
echo trim($result);
Try this:
$words = $item->getLabel(); //array(" Hello", "all ", "people ");
echo trim(preg_replace("/\s+/", " ", implode($words, " ")));
// Output: Hello all people
See demo
Anybody knows how i can achieve this:
I want post message and colorize everything between " * " Tags.
Like this:
This [*]is[*] test [*]message[*] :)
To:
This [yellow]is[/yellow]> test [yellow]message[/yellow] :)
I wroted something like this to achieve my goal:
if(preg_match_all('/\*(.*?)\*/',$message,$match)) {
$beforemessage = explode("*", $message, 2);
$message = $beforemessage[0]. " <font color='yellow'>" .$match[0][0]. "</font>";
}
Howewer it returns only:
This [yellow]is[yellow]
Just use preg_replace():
$message = "This *is* test *message*";
echo preg_replace('/\*(.*?)\*/', '<font color="yellow">$1</font>', $message);
This <font color="yellow">is</font> test <font color="yellow">message</font>
preg_match_all returns an array of matches, but your code only ever replaces the FIRST match in that array. You'd have to loop over the array to handle the OTHER matches.
There are a few approaches when using regular expressions.
One is to do matching - keeping track of the position of the match and the length of the match. Then you can split up the original message into substrings and concatenate it all back together.
The other is to do search/replace using regular expressions.
Try this, or similar approach:
<?php
$text = "Hello hello *bold* foo foo *fat* foo boo *think* end.";
$tagOpen = false;
function replaceAsterisk($matches) {
global $tagOpen;
$repl = "";
if($tagOpen) {
$repl = "</b>";
} else {
$repl = "<b>";
}
$tagOpen = !$tagOpen;
return $repl;
}
$result = preg_replace_callback( "/[*]/", "replaceAsterisk", $text);
echo $result;
i have been dealing with this for awhile and i could not figure out how to do the following in PHP:
$string = "this is test <pre>somrthing in <p>pharagraph</p></pre> dont know tell me <pre>value33 kooo ok</pre> this is php regress <pre>teeth value</pre> ok how";
function get_innercode($string) {
preg_match_all("#<pre>(.*?)</pre>#", $string, $foo);
echo implode("\n", $foo[1]);
}
$insert1=get_innercode($string);
$insert2=" 2 place me in the pre tag"; // you can ignore this
$string="this is test <pre> WHERE $insert1 SHOULD BE</pre> dont know tell me <pre>WHERE $insert2 SHOULD BE</pre> ok how ";
how can i do this?
Please note i cannot do
$string="some text here <pre> WHERE $insert1 SHOULD BE</pre> some more text here <pre>WHERE $insert2 SHOULD BE</pre> ok how";
because i am getting $insert1 and $insert2 from $string to modify. i need to place it back in there where they come from.
thank you
It's pretty easy to do:
$insert1 =" 1 place me in the pre tag";
$insert2 =" 2 place me in the pre tag";
$string = "some text here <pre>{$insert1}</pre> some more text here <pre>{$insert2}</pre> and may be some more text or ";
You can echo a variable from inside a string when it's wrapped around double quotes. This does not work with single quotes.
Edit: This might be what you are looking for then:
$string = "This my <code>awesome</code> code";
$string = preg_replace('/<code>(.*?)<\/code>/', '<pre>$1</pre>', $string);
In php you can concatenate strings with a "." before and after, example:
$var1 = "hello";
$var2 = "What's up";
$concat = "Hey, ".$var1." ,".$var2."<br>";
echo $concat;
Similar methodology here. This allows for larger areas to be filled
$insert[0] = "Some Text";
$insert[1] = "Some More Text";
function Parser($content){
$i = 0;
while (preg_match_all('`\<pre\>(.*?)\</pre\>`', $content, $matches)) {
foreach ($matches[0] as $key => $match) {
//$innertext = $matches[1][$key]; //replace with your preferred filler
$innertext = $insert[$i]; //such as this
$replacement = '<pre>' . trim($innertext) . '</pre>';
$content = str_replace($match, $replacement, $content);
$i++;
}
}
}
Parse your line(s) of content like so:
Parser("this is test <pre> WHERE $insert1 SHOULD BE</pre> dont know tell me <pre>WHERE $insert2 SHOULD BE</pre> ok how");
This is untested so may have a couple of bugs in it.
Try this,
I am adding div tag before each pre content. You can also modify content as needed by using $matches[1]
$string_new = preg_replace_callback(
'#<pre>(.*?)</pre>#',
function ($matches) {
return "<div>".$matches[0]."</div>";
},
$string
);
echo $string_new;