Sorry for my english.
$string = "product#[:id]#[:str]";
$regex = preg_replace("/\[:(\w+)\]/", "(?<$1>.+?)", $string);
What I get is product#(?.+?)#(?.+?) instead of product#(?<id>.+?)#(?<str>.+?)
I want to do that because I need preg_match() to create key names in $matches:
preg_match('/^'. $regex .'$/', str_replace("/", "#", self::$path), $matches)
The point is, it works without '<' and '>'. But I need them for next code.
So what should I do about these '<', '>' chars? I need them but don't know what to do. I also tried to escape them: \< (But no results).
Please help me if you have any ideas.
Edit:
Seems like what I did was ok.
Now there's new problem:
$string = "product#[i:id]#[*:str]";
$regex = preg_replace("/\[i:(\w+)\]/", "(?<$1>[0-9]+)", $regex);
$regex = preg_replace("/\[\*:(\w+)\]/", "(?<$1>.+?)", $regex);
No I tried something else and this doesn't work. Why? :(
If the result of your preg_replace is to be rendered in a browser,
and just this rendered result should contain < and > chars,
then you should generate them as < and >.
So maybe your replacement string (2nd argument of preg_replace) should be:
(?<$1>.+?)
Related
The issue:
Basically when it sees type of letter that regex don't allow it messes up with the link.
My function in php to convert the names that are read from database into links:
function convertActor($str) {
$regex = "/([a-zA-Z-.' ])+/";
$str = preg_replace($regex, "<a href='/pretraga.php?q=$0' title='$0' class='actor'>$0</a>", $str);
return $str;
}
Also I want to allow spaces, dashes, dots and single quotes.
Thanks in advance.
You could try this:
$regex = "/(?:[a-zA-Z.\-' ]|[^\\u0000-\\u007F,])+/";
Which converts régime, Coffehouse Coder, Apple into
'<a href='/pretraga.php?q=régime' title='régime' class='actor'>régime</a>,<a href='/pretraga.php?q= Coffehouse Coder' title=' Coffehouse Coder' class='actor'> Coffehouse Coder</a>,<a href='/pretraga.php?q= Apple' title=' Apple' class='actor'> Apple</a>',
here on regex101.com.
The follwing regex should work for you
[^\u0000-\u007F ]|[a-zA-Z-.' ]\g
[^\u0000-\u007F ] : will match all non English Unicode characters.
[a-zA-Z-.' ]: will match English alphabets
For PHP use this : [^\\u0000-\\u007F]|[a-zA-Z-.']
For everyone that is looking for the solve, i found this temporary solution:
$regex = "/([\w-.' \á-\ÿ])+/";
Without dot and stuff but with spaces:
$regex = "/([a-zA-Z \á-\ÿ])+/";
Cheers
I have a question. I need to add a + before every word and see all between quotes as one word.
A have this code
preg_replace("/\w+/", '+\0', $string);
which results in this
+test +demo "+bla +bla2"
But I need
+test +demo +"bla bla2"
Can someone help me :)
And is it possible to not add a + if there is already one? So you don't get ++test
Thanks!
Maybe you can use this regex:
$string = '+test demo between "double quotes" and between \'single quotes\' test';
$result = preg_replace('/\b(?<!\+)\w+|["|\'].+?["|\']/', '+$0', $string);
var_dump($result);
// which will result in:
string '+test +demo +between +"double quotes" +and +between +'single quotes' +test' (length=74)
I've used a 'negative lookbehind' to check for the '+'.
Regex lookahead, lookbehind and atomic groups
I can't test this but could you try it and let me know how it goes?
First the regex: choose from either, a series of letters which may or may not be preceded by a '+', or, a quotation, followed by any number of letters or spaces, which may be preceded by a '+' followed by a quotation.
I would hope this matches all your examples.
We then get all the matches of the regex in your string, store them in the variable "$matches" which is an array. We then loop through this array testing if there is a '+' as the first character. If there is, do nothing, otherwise add one.
We then implode the array into a string, separating the elements by a space.
Note: I believe $matches in created when given as a parameter to preg_match.
$regex = '/[((\+)?[a-zA-z]+)(\"(\+)?[a-zA-Z ]+\")]/';
preg_match($regex, $string, $matches);
foreach($matches as $match)
{
if(substr($match, 0, 1) != "+") $match = "+" + $match;
}
$result = implode($matches, " ");
I would like to accept in string PHP only letters and change all spaces for plus (+) character. Further I would like to accept only one plus next to each other. So this string:
$string = 'ex,a-mpl*e s++tri ng';
sholud replace for:
$string = 'example+s+tri+ng';
I tried like that:
#[^a-zA-Z0-9\+\]#
but that isn't helpful for me.
Can you help me? :)
EDIT:
aahh, and I would like to also accept only strings where string's lenth is 3 or more.
Try if something like this could be a good starting point:
$string = 'ex,a-mpl*e s++tri ng';
$pattern = array(
'/([^a-zA-Z\+\s])/',
'/(\+|\s)+/'
);
$replacement = array('', '+');
$result = preg_replace($pattern, $replacement, (strlen($string) > 3) ? $string : false);
var_dump($result); //<- return an empty string if the length is < 3
Just letters and digits
You got your regex already, just use it
$str = preg_replace('#[^a-zA-Z0-9\+\]#', '', $str);
Replace Spaces, strip multiple ones
$str = preg_replace('/\s+/', '+', $str);
How to solve such things
I don't want to be rude but the code I pasted, is just a slightly change of Example #4 from here http://us2.php.net/en/preg_replace
So please questions like this are very common and php.net really does a good job with putting examples together. So try to search there first!
So I'm trying to do a string replace and something is happening that I wouldn't expect to happen and wanted to see if someone could shed some light on it.
I'm trying to do a regex replace where I replace '| ' if it is present. I'm using a group matching and the question mark to get it done, but for some reason it's replacing just spaces as well.
$str = 'x x';
$str = preg_replace('/(| )?/','',$str);
echo $str; // Echoes out 'xx' whereas it should return 'x x'
But when I replace a space with a carret I get:
$str = 'x^x';
$str = preg_replace('/(|^)?/','',$str);
echo $str; // Echoes out 'x^x' as expected
Is there some special thing with spaces that I'm not remembering? Or should this just work?
I tried the following:
$str = preg_replace('/(|\s)?/','',$str);
$str = preg_replace('/(|[ ])?/','',$str);
And both of them are also giving the inaccurate results. Thoughts?
Oh, didn't know you were waiting for me xD
As per comment, you should escape the pipe with a backslash: \|.
The | (pipe) is a special character in regex and means 'or', so that your regex were matching either 'nothing' or 'space' in the first one and either nothing or caret ^ in the second one.
Hello i have this code but there is a problem with preg_match on passing $pattern variable.
$pattern = htmlspecialchars($_POST['pregmatch']);
$pattern = "'".$pattern."'";
preg_match($pattern, $content, $m);
if i give this /<span class=\"product_title\">(.*)<\/span>/
in the $_POST['pregmatch'] pregmatch returns NULL.
If i change the code to this
//$pattern = htmlspecialchars($_POST['pregmatch']);
//$pattern = "'".$pattern."'";
preg_match('/<span class=\"product_title\">(.*)<\/span>/', $content, $m);
works like a charm.
So whats the deal here? Slashes creates this problem?
Thanks in advance.
Both of your additional lines appear to be causing a problem... if you were to do:
preg_match($_POST['pregmatch'], $content, $m);
You would probably get the desired result.
htmlspecialchars converts the angle brackets < and > from your input into the HTML escapes < and >. Thus your pattern becomes:
/<span class=\"product_title">(.*)<\/span>/
Which won't match the $content if that's still raw HTML.
Also adding ' around the pattern will screw it up:
$pattern = "'".$pattern."'";
You then had two delimiters.
'/<span class="product_title">(.*)<\/span>/'
Best idea is to use var_dump when unsure about variable state. And enable error_reporting whenever something doesn't work.