Jump to content

ragax

Members
  • Posts

    186
  • Joined

  • Last visited

Everything posted by ragax

  1. Code: <?php $s1='http://domain.com/xxx-c-39.html'; $s2='http://domain.com/xxx-c-38_107.html'; $regex=',http://domain\.com/(?:[[:alpha:]]+-){2}+(\d+)(?:_(\d+))?,'; preg_match($regex,$s1,$m); echo $m[1].'<br />'; preg_match($regex,$s2,$m); echo $m[1].'<br />'; echo $m[2].'<br />'; ?> Output: 39 38 107 Edit to explain a bit: This assumes an underscore between the two numbers. If the separator can be something else, such as a dash, let us know, you'll have to replace the underscore with something like [-_], where the [brackets] contain the allowed separators. In general, if there is anything "fixed" in the regex (e.g. the domain name, the dashes) that you want to make variable, just let us know.
  2. It's a pleasure. You have to work fast when dinner is served. Going offline now, but if the expression gives you any trouble I'm sure someone on the board will be able to help. Wishing you a fun day
  3. Here you go, bro. Input: http://domain.com/xxx-c-39.html http://domain.com/xxx-c-38_107.html Code: <?php $s1='http://domain.com/xxx-c-39.html'; $s2='http://domain.com/xxx-c-38_107.html'; $regex=',http://domain\.com/(?:[[:alpha:]]+-){2}+(\d+),'; preg_match($regex,$s1,$m); echo $m[1].'<br />'; preg_match($regex,$s2,$m); echo $m[1].'<br />'; ?> Output: 39 38 Let me know if this is what you need.
  4. Hi Joe, Perfectly clear. I thought that when you wrote in your second message, you meant that you intended that to be the entire regex. This surprised me as it matches aa. I missed the bottom of that message, where your corrected expression lived. My bad. Your second version of your original regex. For the record, then, this regex matches a.aaaaaaaaaa but not a.aa.aaaaaaaaaa In other words, you can have a ten-letter tld, but only if it is not preceded by a sub-tld. This makes me wonder if you wouldn't prefer the middle part to be the optional one, rather than the last part. Not a big deal, though, and I think I can hear your answer from here ("I stand by my regex" ?) I don't mean to enter a nitpicking contest. For the record, the history of the convo is that at first I thought I saw a bug, and there was one. Then I thought I saw a second bug, and I saw wrong. At this stage if you are happy with the feature above, no probs, just thought I'd bring it up. There are a million ways to match a url, all with their personalities. Nothing wrong with that, it's just nice to "date" a little and get to know a personality before getting married. Wishing you a fun day
  5. Sorry, I meant ~. Next, you can add an optional s: https? instead of http. You can allow ftp if you like: (?:https?|ftp) instead of https? You can add an optional www.: (?:www\.)? You can add optional DOT-subdomains: (?:\.[a-z0-9-]+)* You can add characters to the classes. Etc.
  6. Yes, I gave you the pure regex. To use it in PHP, we just need to wrap it in delimiters. As a working starting point from which we can refine the expression to what you want, try this: $url = 'http://google.com/'; if(preg_match("~^http://[a-z0-9-]+\.[a-z]{2,5}/?$~i", $url)){ echo '1'; } This is borrowing from [url=http://www.phpfreaks.com/forums/index.php?topic=353226.0]another thread from today[/url]. (The delimiter in this pattern is !.)
  7. Hi ixcese, do you think the expression I gave you from the RB library might work for you? Or do you have special requirements?
  8. I like the fact that it matches ftp://A.US.A879__*@@&&=+=+()<script="javascript_hackme"||DROP_MY_TABLE That's highly original for a "web address" checker. I'd suggest starting over and using something off the shelf. Let's see, taking something from the RB library, how about this? \b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|$!:,.;]*[A-Z0-9+&@#/%=~_|$] Not saying that's the perfect regex, but perhaps a better place to start. Wishing you a fun week
  9. Hi Joe, But that matches aa (No tld needed.) So my question still stands: did you mean to say if(preg_match("/^[a-z0-9-]+\.[a-z]{2,5}$/", $domain)) Thank you for your interesting information about the future of tlds, and the link! Wishing you a fun day.
  10. Here is a problem that arose to someone I was helping over PM. I thought I'd post the answer here as PM answers don't help grow the forum's knowledge base. The problem ("idealized"): Why does this return "Nope"? <?php $string='R2'; if (preg_match('~ (?x) # Comments Mode \w # One word character \d # One digit ~',$string)) echo 'Yep'; else echo 'Nope'; ?> Answer: because you turned on comment mode in mid-expression From the starting delimiter ~ to the point where comment mode is turned on (?x), you have a couple spaces and a carriage return. The regex engine first tries to match those spaces and carriage return, then only does it turn on comment mode, then it tries to match a word character and a digit. So R2 will not match. This would work: <?php $string='R2'; if (preg_match('~(?x) # Comments Mode \w # One word character \d # One digit ~',$string)) echo 'Yep'; else echo 'Nope'; ?> Conclusion: In most cases you'll want to have (?x) at the beginning of your patterns. If you have (?x) later on, just be aware that everything before it will be matched in regular (non-comment) mode. Also a reminder: if using comment mode, if you need to match a space character, either use \s or [ ] as the engine will ignore any spaces in the expression past the (?x). Wishing you all a fun week!
  11. Not sure, but it seems to me there might be a slight bug. As Joe says, As it is, the regex matches a.zzzzzzzzzzzzzzz That is because as they are, the last two character classes [a-z]{2,10}([a-z]{2,5})? do not really make sense unless something is missing: apart from the capture (which ankur doesn't seem to care about), the regex above is equivalent to a simple [a-z]{2,15} Joe, did you mean to say: if(preg_match("/^[a-z0-9-]+\.[a-z]{2,5}$/", $domain)) Or am I missing something? Just waking up, so that's entirely possible. Wishing you all a fun Sunday.
  12. You're welcome bro, it's a pleasure.
  13. Hi WickedWolf, You're a star. Thank you for your precious feedback, I will have a look at all these issues.
  14. Okay, so did my last post solve it, or do you need more help?
  15. Without testing: function autolink($description) { $regex=',(http://)?(www\.[-a-z0-9@:%_\+.~#?&//=]+),i'; $description = preg_replace($regex,'<a href="http://\2" target="_blank">\1\2</a>',$description); return $description; } The http only gets inserted if the text if it's there originally. If you want it there all the time: function autolink($description) { $regex=',(?:http://)?(www\.[-a-z0-9@:%_\+.~#?&//=]+),i'; $description = preg_replace($regex,'<a href="http://\1" target="_blank">http://\1</a>',$description); return $description; }
  16. Okay, you're changing the spec on me. Slight change to the regex then, with an optional http:// $regex=',(?:http://)?(www\.[-a-z0-9@:%_\+.~#?&//=]+),i'; I have to tell you that the movement is away from the ereg functions, now deprecated, and toward the preg functions. In a future version of PHP, they will drop off. I would guess that this would fix your code (didn't have time to test it though, past midnight here): $description = preg_replace('~([_.0-9a-z-]++@([0-9a-z-]{2,}+\.)+[a-z]{2,3})~i', '<a href="mailto:\1">\1</a>', $description); Let me know if that works for you.
  17. Yes, so in your function, just pop in what I gave you. Without doing anything else to your code: function autolink($description) { $regex=',([[:space:]()[{}])(www\.[-a-z0-9@:%_\+.~#?&//=]+),i'; $description = preg_replace($regex,'\1<a href="http://\2" target="_blank">\2</a>',$description); return $description; } Or, for the second version, in case Group 1 in your regex is leftover garbage from something else: function autolink($description) { $regex=',(www\.[-a-z0-9@:%_\+.~#?&//=]+),i'; $description = preg_replace($regex,'<a href="http://\1" target="_blank">\1</a>',$description); return $description; }
  18. Hi eMonk, Not sure where the first capture group fits in real life, but keeping it as is (only modifying Group 2), this works. Note that the input string starts with a } to give the regex something that matches the first capture group you specified. Input: }www.test.com Code: <?php $regex=',([[:space:]()[{}])(www\.[-a-z0-9@:%_\+.~#?&//=]+),i'; $string='}www.test.com'; echo htmlentities(preg_replace($regex,'\1<a href="http://\2" target="_blank">\2</a>',$string)); ?> Output: }<a href="http://www.test.com" target="_blank">www.test.com</a> If the first capture group is "old garbage", run this instead: Input: www.test.com Code: <?php $regex=',(www\.[-a-z0-9@:%_\+.~#?&//=]+),i'; $string='www.test.com'; echo htmlentities(preg_replace($regex,'<a href="http://\1" target="_blank">\1</a>',$string)); ?> Output: <a href="http://www.test.com" target="_blank">www.test.com</a> Is this what you're looking for?
  19. On Windows, I recently dumped Filezilla for the FTP function in Directory Opus---not a free program, but a powerful file manager replacement. I'd used Opus for years, but the FTP function didn't really work for me until version 10. Now it's awesome. I basically have Opus running all the time---it's my file manager: a dual file lister and multiple tabs. FTP sites are just one more tab. Opus treats FTP sites like other folders on the file system, so you can drag and drop from any tab to an FTP folder. It's a bit hard to explain unless you see it, see the link below for screenshots and links to the demo on the official site. You can also synch the local and remote folder, so when you drill down on one side, the other follows. I have shortcuts to launch two of my top FTP sites. The shortcut opens two tab, one for local and one for remote. For more, here's the section of my Opus tut about Opus FTP.
  20. Hi everyone, I just went through a major revamp of my regex tutorial site: http://www.asiteaboutnothing.net/regex/ Several questions are going through my mind: - regex turns a lot of people off, so is it understandable for "normal" coders? - not sure yet if I've hit on the final title---does it work? - are there things you'd do to improve the presentation? Also working on more material, but all suggestions are welcome. Wishing you a fun weekend. With gratitude,
  21. Strangely, the sticky does not mention NetBeans, which is a strong candidate for son.of.the.morning. I use NetBeans, EditPadPro and Dreamweaver. You can get Dreamweaver to treat all kinds of files like php. That's what I do for my html files, as they are parsed by PHP.
  22. Sweet, great to hear. Wishing you a relaxing weekend.
  23. You won't regret it, they're heaps of fun. Yes. (Try it! Just change $string in the code I sent you.) Basically, we defined a pattern called "Cap". We capture that pattern four times. Cap is whatever is present within these four sets of parentheses, including the parentheses themselves: \((?>[^)]+\) So is this solved? Meaning, are you happy with the version that extracts the data from one-line input?
  24. Hi knot13yet, From your question, I wasn't clear if you were trying to extract the numbers from one specific line, or from the whole block of lines you posted. Let me know, in the meantime here is code to extract your data from one specific line. Input: 4 4 0 -1 ( (0 0) (4 4) (6 6) (2 2) ) ) Code: <?php $regex=',(?x) (?(DEFINE)(?<Cap>\((?>[^)]+\)))) (?>(?:-?\d\s){4}\([ ]) ((?&Cap))\s((?&Cap))\s((?&Cap))\s((?&Cap)),'; $string='4 4 0 -1 ( (0 0) (4 4) (6 6) (2 2) )'; if(preg_match($regex,$string,$match)) { echo $match[2].'<br />'; echo $match[3].'<br />'; echo $match[4].'<br />'; echo $match[5].'<br />'; } ?> Output: (0 0) (4 4) (6 6) (2 2) I thought this would be a great chance to demo the DEFINE feature from my post yesterday about two little-known php regex features.
  25. Hi Joe, You don't know the number of spaces (if any) in the string, is that right? If so, your preg_replace_callback combined with a str_replace or preg_replace looks great to me. If you know the number of spaces, you can do it with a single preg_replace, but you probably know that. Wishing you a fun weekend.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.