preg_match and remove multiple characters in string? - php

Hi I'm using php to program my site and I've been reading loads about preg_match and trying lots of examples. What I'm trying to do is something like below...
if(preg_match('These characters'), $myVariable, matches)){
Find and remove found characters from $myVariable;
}
I'm pretty sure this is obvious to php experts but it's had me stuck after hours of trying and reading.
Thanks in advance

You don't need to check for a match before doing a replace. It's like if you were to do:
str_replace("A","B","ZZZZZZZ");
It just won't replace anything. Same goes for preg_replace: If there is no match, it just does nothing.

It sounds like you should be using preg_replace. If you wanted to remove all y's and o's for example you would do this:
$string = 'hey you guys!';
$ans = preg_replace('/[yo]/','',$string);
print_r($ans); //outputs 'he u gus!'
Whatever characters you want to remove, just put them between the brackets [...]

Related

How to do this PHP find-replace

I think I need to use the preg_replace function but not sure exactly how to type in the patterns I want to find and replace. Basically, I want to replace this:
: u"x"x",
with this:
: u"x'x",
x means that any characters can go there. But I don't know how to write the x in PHP.
Thank you!
Edit: basically, I want to replace that middle double-quote with a single-quote. And I'll be searching through a big JSON file to do it. Probably should have said this at the start.
You could use this regular expression:
$result = preg_replace('#(: u".*?)"(.*?")#', "$1'$2", $string);

preg_replace multiple times in same string

I have a text and I want to do something like Wiki code, creating links with [[]] and stuffs.
I am using this preg_replace to do that, and it seems to work:
<?=preg_replace("/\{\{([^\*]+)\|([^\*]+)\|([^\*]+)\}\}/", "<a href='$1.php#$2'>$3</a>", $conditions['pattern']); ?>
The problem is that when I have this text "can[not] build at %{{types|location|location}}% %{{some|other|stuff}}%" it outputs this:
can[not] build at %stuff%
It's like only the last one gets replaced, but wrong.
Any idea? Thanks
Fixed!
I changed the regular expression to /\{\{([a-zA-Z]+)\|([a-zA-Z]+)\|([a-zA-Z ]+)\}\}/ and now it works :D

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.

Get last word in URL and delete what is not necessary

Great guys have helped me a lot in this other post:
Get last word from URL after a slash in PHP
Now I am facing another issue.
How can I say something like this in PHP:
if in $last_word you find also -0.htm then delete -0.htm
I explain my $last_word now shows test-0.htm
But I do not need -0.htm
I only need "test".
How do I say in PHP to delete -0.html and to grab only "test".
Thanks for your help. Since it is a dynamic script I obviously do not know what is before "-0.html". The word "test" is only an example. Just to let you know that "test" is represented by a variable in my code, and it works. Now I only need to tell to the code to eliminate 0.html when is found.
THANK YOU
$last_word = str_replace(array('-0.html','-0.htm'), '', $last_word);
Hope this helps. It will replace the string (-0.html or -0.htm) even if found somewhere else than at the end of $last_word.
Trying to remove -0.htm at the end of $last_word:
Regex is an easy way to go:
$last_word = preg_replace('/\-0\.htm$/', '', $last_word);
The $ means 'the end', which means the -0.htm has be be at the end.
If instead of -0.htm you want to remove -0.html:
$last_word = preg_replace('/\-0\.html$/', '', $last_word);

Regexp for cleaning the empty, unnecessary HTML tags

I'm using TinyMCE (WYSIWYG) as the default editor in one of my projects and sometimes it automatically adds <p> </p> , <p> </p> or divs.
I have been searching but I couldn't really find a good way of cleaning any empty tags with regex.
The code I've tried to used is,
$pattern = "/<[^\/>]*>([\s]?)*<\/[^>]*>/";
$str = preg_replace($pattern, '', $str);
Note: I also want to clear &nbsp too :(
Try
/<(\w+)>(\s| )*<\/\1>/
instead. :)
That regexp is a little odd - but looks like it might work. You could try this instead:
$pattern = ':<[^/>]*>\s*</[^>]*>:';
$str = preg_replace($pattern, '', $str);
Very similar though.
I know it's not directly what you asked for, but after months of TinyMCE, coping with not only this but the hell that results from users posting directly from Word, I have made the switch to FCKeditor and couldn't be happier.
EDIT: Just in case it's not clear, what I'm saying is that FCKeditor doesn't insert arbitrary paras where it feels like it, plus copes with pasted Word crap out of the box. You may find my previous question to be of help.
You would want multiple Regexes to be sure you do not eliminated other wanted elements with one generic one.
As Ben said you may drop valid elements with one generic regex
<\s*[^>]*>\s*` `\s*<\s*[^>]*>
<\s*p\s*>\s*<\s*/p\s*>
<\s*div\s*>\s*<\s*/div\s*>
Try this:
<([\w]+)[^>]*?>(\s| )*<\/\1>

Categories