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);
Related
I'm trying to scrap some information just for learning PHP and regex and I would like to extract it from an html.
The html text is an entire webpage but it has some patterns like somehtmltext_andtags_andeverything /ajax/hovercard/user.php?id=THE_ID_I_WANT andmore_text_and_tags.
I can isolate the pattern with TextEdit in Mac, but I want separate it!
how could I make it in PHP?
Thank you in advance!
Rafael.
Sorry, I was very unclear.
I want to separate only de ID, so if you see the image, the only text you would get is 100009799451329 . If the final result is the whole sentence (ajax/hovercard/user.php?id=100009799451329) it doesn't matter, goes fine for me!
try this
$matchArr = NULL;
preg_match_all("/\/ajax\/hovercard\/user\.php\?id=(.*?)\&/", $yourStr, $matchArr);
print_r($matchArr);
You can use the following pattern to find the id:
\/ajax\/hovercard\/user.php\?id=(\d+)
See a demo.
Explanation:
\/ajax\/hovercard\/user.php\?id= will match /ajax/hovercard/user.php?id=
(\d+) captures a sequence of digits, in this case the user id.
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 [...]
I need to get a number from this website: Current STC price which displays a market driven figure: STCs.
i tried this:
$html = file_get_contents('http://www.greenenergytrading.com.au/certificates/todays-pricing');
$html = strip_tags($html);
which leaves me with a long string. I then tried to remove anything before the figure I'm after, assuming that the text wont change:
$html = preg_replace('/.*Current STC price/', '', $html);
However, this doesnt work. it seems to work on online RexExp tester but not in production. also, is this a reasonable approach?
cheers
You can use preg_match with the $matches parameter provided to extract all ocurrences of from the website source and store them in an array. Then, just access the first element of the array.
Check out the documentation for preg_match here:
http://php.net/manual/en/function.preg-match.php
EDIT: Oh, I just saw your comment that you tried preg_match already. What regexp's have you tried? Have you tried something like "/$[0-9]{1}/" ?
I'm writing a small CMS and I'm trying to turn a title into a URL slug with dashes. I know I need to do a couple of things and I've got the whole thing work, but I just don't like it. The problem seems to be that if there are any special characters at the end, I'd need to remove them before it goes into the database. The only way I could figure out doing this was to do 2 preg_replace's in one statement. So it looks something like this:
preg_replace("/\-$/","",preg_replace('/[^a-z0-9]+/i', "-", strtolower($title)));
and it and turn this: (this is a title!!!)))**that is (strange))
into this: this-is-a-title-that-is-strange
But this expression just looks like ass. There has to be a better way of coding this, or something out there, I just don't know it. Any help would be greatly appreciated
You can make just one call to preg-replace with array inputs as:
preg_replace( array('/[^a-z0-9]+/','/^-|-$/'), // from array
array('-',''), // to array
strtolower($title));
Note that your existing code retains leading - if any. The code above gets rid of that.
One option, which still requires two replacements but takes care of both the start and end dashes in one pass, is:
preg_replace('/[^a-z0-9]/', '',
preg_replace('/([a-z0-9])[^a-z0-9]+([a-z0-9])/', '$1-$2',
strtolower($title)));
There is also the alternative of:
implode('-',
preg_split('/[^a-z0-9]/',
strtolower($title),
PREG_SPLIT_NO_EMPTY));
Use trim.
trim(preg_replace('/[^a-z0-9]+/i', "-", strtolower($title)), '-')
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   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>