-
Posts
14,780 -
Joined
-
Last visited
-
Days Won
43
Everything posted by .josh
-
backslashes are used to escape things. For instance, if you have this: $string = "some "random" thing"; you are going to get a parse error, because php will think the 2nd quote is the end of the string. In order to tell php that no, that's not the end of the string, you escape it like this: $string = "some \"random\" thing"; That is the general principle of the backslash. Within a regex pattern, there are several things that need to be escaped. For one thing, quotes you may be trying to match within the pattern, just like I mentioned above. I don't have the quotes escaped in the pattern I gave, because I used single quotes around the pattern. Since I used single quotes, the double quotes don't need to be escaped, because php doesn't match single quotes to double quotes like that. Now, if there was a single quote in the pattern, I would have had to escape it, since I used single quotes around the pattern. Next thing is the pattern delimiter. The delimiter is what tells the regex engine what the start and end of the pattern is. You can use pretty much any non-alphanumeric character for the pattern delimiter. DJ chose to use / as the delimiter. Since he chose to use that, he has to escape any instance of that in the pattern (like in closing html tags), so that the regex engine knows for instance the / in </a> is not the end of the pattern, but part of the pattern. So it would have to look like this: <\/a>. / is a pretty common character to popup in patterns, because running regexes on html content is pretty common. I usually use ~ because it is a character that doesn't come up often, and instantly makes one less thing I have to escape in the pattern, as far as dealing with html content. On top of that, putting a backslash in front of certain things denotes special characters. For instance, \n stands for a new line. \s stands for a space or tab. \d stands for a digit. \w stands for any lower or uppercase letter or underscore. There are several things in DJ's regex that do not need escaping, because he doesn't use them as delimiters, nor do they mean anything special to the regex engine (=, >, and <) Escaping them doesn't necessarily hurt anything, but it makes for an ugly regex and also gives away noobness ([^"]*) means to match and capture 0 or more of anything that is not a ". It's pretty simple and straight forward. Is the next character a "? No? okay it matches. Keep on going. (.*?) means to match and capture 0 or more of anything except a new line, unless you use a modifier to tell it to match new lines too. It will keep matching until it reaches the first instance in which the rest of the pattern after it can be matched. So in order for it to get a final match, the engine must constantly look ahead and keep back tracking until it finds that first instance. Then it has to turn around and walk through the string all over again, for the rest of the pattern. So the really really short answer is the first one is more efficient and less likely to produce unexpected matches, so you should use negated character classes ([^]) instead of nongreedy match-alls (.*?) whenever possible.
-
'~<td class="file">\s*<a href="([^"]*)" title="([^"]*)">(.*?)</a>~is'
-
[SOLVED] Does session_start have to be called inside a function?
.josh replied to therealwesfoster's topic in PHP Coding Help
hmm I coulda swore I saw a question mark in there, hence the statement. My bad. But for argument sake, for #1, I meant it in a more generic sense. As in, if you're going to take the time to put it in one place, why not just put it in another place, click the proverbial run button and see what happens? -
[SOLVED] Does session_start have to be called inside a function?
.josh replied to therealwesfoster's topic in PHP Coding Help
Instead of c/p'ing it here and asking if it will work, you could always c/p it to a test.php and run it and find out. -
[SOLVED] Does session_start have to be called inside a function?
.josh replied to therealwesfoster's topic in PHP Coding Help
session_start() needs to be called before you use a session variable. Doesn't matter if it's inside or outside of a function. session variables are already global. If you assign something to a session variable without (or before) calling session_start(), it will act like a normal global variable for that page, but will not persist to other pages. -
lots of reasons that could happen. You could have upgraded to a new/diff php version that has error reporting set to show notices. You could have added script somewhere that turns it on. You could have deleted a var someone or reordered something to where that variable is not defined at that point in time (and error reporting set to that level). It's just a notice, saying that the variable has not been declared. You can make it go away be declaring it before using it, turning error reporting off on notices, or just ignoring it (because php allows you to use variables on the fly like that).
-
You can't have multiple delimiters for explode. That's what preg_split is for. But even then, it explodes at the delimiter, so you will get sentences returned without the punctuation marks. You can take preg_split a step farther and flag it to return them in their own elements with PREG_SPLIT_DELIM_CAPTURE and then run some loop to implode sentence and following punctation mark in the returned array, or just use preg_match_all: preg_match_all('~.*?[?.!]~s',$string,$sentences);
-
I see a "would you.." at the beginning, but I see no question marks at the end of that statement, which makes it rhetorical, as in, you aren't really asking, you're just being nice about telling. You are politely telling him to do your work for you and make sure it's pretty. But I'm not even gonna argue semantics here. Let's just assume you intended it to be an honest question/request. Posting looking for people to do stuff for you, even if it's in the form of a question, and even if it is polite, usually never ever yields positive response. All it does is make you look lazy and want other people to do your work for you. It is usually taken offensively, especially when you throw in things like "and make sure it's 'tidy'". The point I'm making is that we aren't here to do people's work for them. We are here to help people figure it out for themselves. If you want someone to do your work for you, I suggest hiring someone.
-
[SOLVED] help with replace dynamic url into static url
.josh replied to irkevin's topic in Regex Help
$string is a generic variable, used to show the 3rd argument in str_replace. $subject is also a common one. In other words, you need to put your own variable in there. Whatever variable that has your url you want changed. -
seriously... you don't see anything wrong with this picture? This says "do it for me, and make sure it looks good while you're at it."
-
preg_match('~<b class=" yfi-price-change-down">.*?</b>~',$string,$match); print_r($match);
-
[SOLVED] help with replace dynamic url into static url
.josh replied to irkevin's topic in Regex Help
if you want to remove index from it, just do str_replace('/index/','/',$string); -
I think the point of the last couple of posts that you seem to have missed is that we aren't here to do your work for you and it doesn't help that many of us (myself included) took your posts as you going beyond even asking for someone else to do it (not okay), and demanding/expecting it (even worse).
-
whole file is being retrieved because you are using fread, which returns x bytes length of data, and you are requesting info of filesize($filename) length (the whole file). show an example of the file contents, and what part you want retrieved from it.
-
why don't you try doing it like the manual suggests. I think they know how to do it better than you.
-
example #3 in the manual entry for date shows you how to do that. It even gives +1 year as the example. Doesn't hurt to actually read the manual, you know.
-
$NAME = ($row['NAME'] == '' || $row['NAME'] == false || is_null($row['NAME']) ? 'No name' : $row['Name']; Better thing to do would be to make 'No name' the default value in your database.
-
you can use $_SERVER['HTTP_REFERER'] but that's not 100% foolproof. Alternatively, if both pages are on your server, you can create a session var on page 1 and check for it on page 2. If its not there, they obviously didn't go to page 1 first.
-
if you have the chmod permissions, unlink
-
sarchasm ftw.
-
Yeah KingPhilip, and he wants it on his desk by Friday, or you're fired!
-
Well i would not call it lazy when i've been searching for hours to find the answer and didn't. Thanks to the other person who responded and didn't call my issue lazy. haha doh!
-
preg_match('~<TABLE BORDER=1 cellspacing=0 cellpadding=3>(.*?)</TABLE>~s',$content,$matches);
-
[SOLVED] simple problem with mkdir....please help me!!
.josh replied to tbarbedo's topic in PHP Coding Help
you also need to escape the backslashes so php doesn't think you're trying to pass special chars -
[SOLVED] simple problem with mkdir....please help me!!
.josh replied to tbarbedo's topic in PHP Coding Help
looks like you're running your script in a windows environment, so you need to format your path/to/dir to follow windows format.