MySQL LIKE Statement and PHP Variable - php

I need my following code to work.
I am trying to use a PHP Variable and also add a [charlist] Wildcard statement to it
Could some one please help figure out why it wont work.
Basically it works if i remove the [charlist] Wildcard but I need it to find all the letters which are in the PHP Variable
my code is as followed
LIKE ''[' $searchWord ']%''

To use a character class, you need to use the REGEXP operator.
Additionally, after a character class, you need to indicate a repetition operator. % matches any string (and is only for LIKE), but if you want to apply it so that it will match any series of letters contained within your character class, you need to do:
$query = '`column` REGEXP "[' . $searchWord . ']+"';
Alternatively, use a * to match 0 or more. (+ is 1 or more)

If $searchWord is an array, try it by calling implode first on it:
$listOfCharacters = str_split($searchWord, 1);
$implodedString = implode(',', $listOfCharacters;
The imploded string is a comma seperated string now instead of an array. PHP doesn't convert arrays to string by itself.
Then you can probably use it like:
LIKE ''[' $implodedString ']%''
Although I'm suspicious about using it without string concatonation here. Do we miss some piece of code here?

Related

Using preg_replace to alter youtube links

I want to change the standard youtube video links (like: https://www.youtube.com/watch?v=zVzhpkFBFP8) stored in my database to embeded urls (like: https://www.youtube.com/embed/zVzhpkFBFP8) using preg_replace. This is my code so far:
<?php
$Link = getuser($my_id, 'YoutubeLink');
$LinkNew = preg_replace("/watch?v=*/","embed*/","$Link");
echo "$LinkNew" ?>
But it isn't working. I'm probably doing something stupid but I'm new to php so any help is appreciated.
No need to use preg_replace for something like that in which there is no pattern. As watch?v= is always the same, instead use str_replace('watch?v=', 'embed/', $Link);
Your regex as is doesn't work because the ? is a special character in regex so you need to escape it. The question mark makes the preceding character optional. To escape just add a backslash before it \?.
The * also is being used somewhat incorrectly. The asterisk is a quantifier so you are saying zero or more equal signs. If you wanted everything after the equals sign you'd do .*. That would get every other character because . is any character and paired with the * is everything. You don't actually want to do that either because you aren't grouping that and the replace would just delete it. If you were to group that you could use that value later in the replace using $1. Here's a write up on that http://www.regular-expressions.info/brackets.html.
<?php
$Link = getuser($my_id, 'YoutubeLink');
$LinkNew = preg_replace("/watch\?v=/","embed/", $Link);
echo "$LinkNew"; ?>
As #V13Axel has pointed out though this can be done just as easily using str_replace.

PCRE replace multiple values with multiple strings

I am trying to mass replace some keywords in a csv file, for example I have a list of keywords cat,mouse,dog with i would like to replace with something,else,here , I am currently using this http://phpcsv.sourceforge.net/phpcsv-1.0.php it is perfect and it says it uses PCRE for replacing values , my question is what do i need to type in the search and replace field to achieve this result ?
You could use
Search (?<=^|,)(cat|mouse|dog)(?=,|$) Replace ${1}2
The ${1} is used to reference the string captured by the () in the Search field.
Normally you could just use $1, but because it is followed immediately by 2, the 1 needs to be enclosed in {}.
If the values may be enclosed in " add "? before and after (cat|mouse|dog).
(?<=^|,) means looking behind there must be the start of a line or a comma.
(?=,|$) means looking ahead there must be a comma or the end of a line.
If the replacements are different for each keyword, I think you would have to do each separately, e.g.
Search (?<=^|,)cat(?=,|$) Replace hamster
Alternatively, if using your own code you could make all the replacements in one go by passing arrays as arguments to preg_replace.

Dreamweaver Regex Find and Replace Using Regular Expression

I am using a Regular Expression to perform a find and replace with dreamweaver. I am running into some difficulty. This is what I have in my page (note that there is a syntax error because I need an additional parenthesis at the end of the string).
$email=htmlspecialchars(mysql_real_escape_string($_POST['email']);
$name=htmlspecialchars(mysql_real_escape_string($_POST['name']);
I am trying to performa a find and replace that will produce this:
$email=htmlspecialchars(mysql_real_escape_string($_POST['email']));
$name=htmlspecialchars(mysql_real_escape_string($_POST['name']));
This is what I am using to perform the find. It seems to be replacing too much text (it starts with the $_POST from the $email variable, but continues all the way down to the $_POST for the $name variable)
Find: \$_POST['([^<]*)']
Replace: $_POST['$1'])
I end up with this:
$email=htmlspecialchars(mysql_real_escape_string($_POST['email']);
$name=htmlspecialchars(mysql_real_escape_string($_POST['name']));
As you can see, it only fixes the last instance (this is because the find function is selecting both lines from $_POST['email'] all the way to $_POST['name']). Any ideas on how to fix this? Thank you!
Add a question mark to make it non-greedy. Also, you need to escape the [ and ] characters that you want to match.
Find: \$_POST\['([^<]*?)'\]
Replace: $_POST['$1'])
Or, alternatively, user a ' character instead of a < character to match the value within the quotes:
Find: \$_POST\['([^']*)'\]
Replace: $_POST['$1'])

RegExp - How to return a single row of answers instead of multiple rows of answers

I have a RegExp
/h.*e.*l.*l.*o.*/i
And I have my paragraph that its being run on.
Hey look at that lion!
The result is this:
answer[0] = Hey look at that lion!
answer[1] = H
answer[2] = e
answer[3] = l
answer[4] = l
answer[5] = o
I was wondering if I could get a result like this:
answer[0] = Hey look at that lion!
answer[1] = Hello
by only changing my RegExp. Maybe using grouping or something? And if the answer to that question is no, then what are my other options? I really don't want to loop over the answer and string it together either, but if that's the only way then I will do it like that I suppose.
Your expression is fine. You're just executing it once and returning one match. Thats fine.
Look here for reference: http://regex101.com/r/jU3zU2
You can use backreferences as I have in the example to get the entire data, if you like.
However, you want to return "hello" which is what you have in the regex... so uh.. what?
This is not possible using a regex. One of the unchangeable rules while using a regex is that every character can only be matched one time. That means you cannot return your whole sentence and some parts within it.
Example:
"abc".match(/a|b|c|(abc)/g)
The last abc group, although it would match, is never matched because the characters of abc match the earlier expressions of a|b|c
I do not know what you want to achieve by essentially hardcoding your result within a regexp, I assume that your real code should do something else. If not, just assign "hello" to a variable.
array_shift($answer);
$answer = implode('',$answer);
The array_shift will remove the element that contains the entire string from the array of results then the implode will bunch them all together into a single string.

String parsing - replace each repeated occurrence of a substring with a unique replacement

I am trying to parse a string. The catch is each of the variables in the string may occur more than once, and I need to replace each repeated occurrence with a unique replacement.
example $string = "$Pronoun $Adjective $Noun is as $Adjective as an $Adjective $Noun"
I've tried str_replace("$Pronoun", getRandomWordByType('Pronoun'), $string)
This works apart from the fact that each occurrence of "$Pronoun" gets replaced with the same pronoun retrieved from a single call to my getRandomWordByType('Pronoun') method.
My objective is to build interesting sentences dynamically, replacing placeholders with words retrieved from a database of words, that are categorised by type...
Thanks in advance for any suggestions :)
Try this:
preg_replace_callback("(\$([a-z]+))i",function($a) {return getRandomWordByType($a[1]);},$string);
This will automatically convert any keyword of the form $Something by passing Something to the getRandomWordByType function. Another advantage is that the random word function is called once for each word.
To prevent accidental replacements, for example $NotAKeyword, have getRandomWordByType return '$'.$keyword (where $keyword is the function's argument) if it can't find the keyword in the valid list.

Categories