Altering search string for full word searches - php

I have a search string like this ...
shop.php?querytp=&query22=&query20=&query21=&querytp1=&querytp2=Shed&querytp3=
which does the job up to a point but I would like to stop the search from finding words within words so that a search for 'shed' doesn't pull up 'brushed' or 'polished' for instance. If I add + to the URL manually so that it becomes ...
shop.php?querytp=&query22=&query20=&query21=&querytp1=&querytp2=+Shed&querytp3=
that does the trick. Lots of the queries are empty at times.
Can htaccess sort this out for me? Bit beyond me this to be honest with you.
Thanks

You could use the php function preg_match to handle your search.
Something like:
preg_match("~\b [$your_search_value] \b~",$were_to_search)
Example:
https://stackoverflow.com/a/16283883/4601619
Read more about the preg_match function on:
http://php.net/manual/en/function.preg-match.php

Related

PHP: How would I remove parts of a string between 2 chunks of characters without removing too much?

This problem is driving me nuts. Let's say I have a string:
This is a &start;pretty bad&end; string that I want to &start;somehow&end; display differently
I want to be able to remove the &start; and &end; parts as well as everything in between so it says:
This is a string that I want to display differently
I tried using preg_replace with a regular expression but it took off too much, ie:
This is a display differently
The question is: how do I remove the stuff just between sets of &start; and &end; pairs and make sure that it doesn't remove anything between any &end; and &start; segments?
Keep in mind, I'm working with hundreds of strings that are very different to each other so I'm looking for a flexible solution that'll work with all of them.
Thanks in advance for any help with this.
Edit: Replaced dollar signs with ampersands. Oops!
Try this regex /\&start;(.+?)\$end;/g
It looks like it works as desired: https://regex101.com/r/MW5nom/2
I quickly tried it on chrome console using JS, tried converting it into PHP:
"This is a &start;pretty bad$end; string that I want to &start;somehow$end; display differently".replace(/\&start;(.+?)\$end;/g, "")

Don't know how to write this preg

I'm making a function call to a library that is returning a malformed json array. I can work around this if I can get a preg written to extract the part that I want.
The array is a jumbled mess, but buried deep inside it is a string that looks like this:
token=??????,
I need to write a preg to grab the characters represented by the question marks. I wrote this, but it's not getting the part of the text that I want:
$token = preg_match('#^(?:token=)?([^,]+)#i', $badJson, $matches);
Can anyone help me? Thanks.
You can try:
/token=([^,]+)/i
and the use the first sub-match to extract the token. Being more specific is usually a good idea with regex (eg. does the token have a set length? does it only contain hex characters? etc.)
Site note: https://leaverou.github.io/regexplained/ is a great site for testing regular expressions.

Using preg_match_all to filter out strings containing this but not this

im having an issue with preg_match_all. I have this string:
$product_req = "ACTIVE-6,CATEGORY-ACTIVE-8,CATEGORY-ACTIVE-4,ACTIVE-9";
I need to get the numbers preceded by "ACTIVE-" but not by "CATEGORY-ACTIVE-", so in this case the result should be 6,9. I used the statement below:
preg_match_all("/ACTIVE-(\d+)/", $product_req, $this_act);
However this will return all the numbers because all of them are in fact preceded by "ACTIVE-" but thats not what i meant because i need to leave out those preceded by "CATEGORY-ACTIVE-". How can i configure preg_match_all to do it? Or maybe there is some other function that can do the job?
EDIT:
I tried this:
preg_match_all("/CATEGORY-ACTIVE-(\d+)/", $product_req, $this_cat_act);
preg_match_all("/ACTIVE-(\d+)/", $product_req, $this_act);
$act_cat = str_replace($this_cat_act[1],"",$this_act[1]);
it kinda works, but i guess there is a better and cleaner way to do it. Besides the output is kinda weird too.
Thank you.

How To Regex Search and Replace array_key_exists with isset?

Whats the best way to do a regex search and replace for all instances of array_key_exists() with the more efficient isset()? Please, no Donald Knuth quotes regarding optimizations and yes, I'm aware of the differences between the two functions.
This is what I'm currently using in my Netbeans search and replace:
search for:
array_key_exists\s*\(\s*'([^']*)'\s*,([^)]*)\)
replace with:
isset($2['$1'])
it works well , changing this:
array_key_exists('my_key',$my_array)
to
isset($my_array['my_key'])
but doesn't pick up instances like this:
array_key_exists($my_key,$my_array)
Not the most elegant solution, but adding to your current regex we find both types of search criteria.
array_key_exists\s*(\s*'|$['|\S]\s*,([^)]*))
The best I could do was to run a second search and replace to cover the instances that used variables for both arguments:
array_key_exists($my_key,$my_array)
search and replace 2:
search for:
array_key_exists\s*\(\s*(\$[^,]*)\s*,([^)]*)\)
replace with:
isset($2[$1])
If you need a WIDER spectrum when upgrading the PHP version rather than JUST this upper use case:
Didn't clean it up, but it should catch every instance I could think of.
Search:
array_key_exists\s*\(\s*([^,]*)\s*,\s*((\(\w+\))?[a-z0-9_$'"\{\}\[\]\-\>\:]*(\(\))*[a-z0-9$_\.\{\}\'\"\[\]\-\>\:]*)\)
Replace:
isset($2[$1])

How to find text in webpage through PHP?

I have a pure text file without any HTML formatting. I want to search for a word in it? how should i do that? and I also want the next words before a comma. can i do that to?
so means:
if its: "word1":"i like stackoverflow", next thing
i want to find word1 which is in inverted commas and then i want the whole phrase i like stackoverflow without the inverted commas. and it should stop at that point.
is there any way to do that?
thanks...
Use a regular expression. In PHP, you can use a function like preg_match to do this.
For the pattern you described, your regular expression code could be something like:
$match_array = array();
$string_to_match = // load the string from a file and put it in this variable
preg_match('/"([A-Za-z0-9\s]+?)":"([A-Za-z0-9\s]+?)"/', $string_to_match, $match_array);
The results of the match will then be placed in $match_array.

Categories