Jump to content

ragax

Members
  • Posts

    186
  • Joined

  • Last visited

Everything posted by ragax

  1. Couldn't resist posting working php code for this: <?php $string = 'STUB>AnotherOne.gh</a>'; if (preg_match('~(?x) # comment mode ( # Start group 1 capture: the whole url without .gh STUB> # This is the part of the url up to > ( # Start Group 2 capture: this is the file name without .gh # On the line below, you could use (?: instead as it is not intended to be capturing ( # Expression "A": Zero or More times... (set by the * at the end) [^.<]*? # Lazily Match characters that are neither dots nor <, expanding as needed \.? # Then match one dot if available, but give it back if necessary to complete the overall match )* # End Expression A that has repeated zero or more time # Expression A has matched a series of zero or many stuffDOT, more_stuffDOT, but gives up the last DOT to allow .gh to match. ) # End Group 2 capture ) # End group 1 capture \.gh # Match .gh (but dont capture)~', $string,$match)) { echo "Match: ".$match[1]."<br />"; echo "File: ".$match[2]."<br /><br />"; } ?> Ouput: Match: STUB>AnotherOne File: AnotherOne
  2. Hey McK, Great to hear from you, and to hear that the expressions from Aba and myself are helping with your project. Sure! Here is a commented / unrolled version, using comment mode (aka whitespace mode). (This expression will actually work in preg_match if you put it inside a pattern string with some delimiters.) (?x) # comment mode ( # Start group 1 capture: the whole url without .gh STUB> # This is the part of the url up to > ( # Start Group 2 capture: this is the file name without .gh # On the line below, you could use (?: instead as it is not intended to be capturing ( # Expression "A": Zero or More times... (set by the * at the end) [^.<]*? # Lazily Match characters that are neither dots nor <, expanding as needed \.? # Then match one dot if available, but give it back if necessary to complete the overall match )* # End Expression A that has repeated zero or more time # Expression A has matched a series of zero or many stuffDOT, more_stuffDOT, but gives up the last DOT to allow .gh to match. ) # End Group 2 capture ) # End group 1 capture \.gh # Match .gh (but dont capture) Note that this exact regex will work on STUB>AnotherOne.gh</a> It is the original expression minus everything up to the >. I hope this answers your question, please don't hesitate to ask if any of it is unclear!
  3. Edit: I have it in reverse. Aba's is the faster one.
  4. Hi McK! aba is right that lookaheads are a nice way to do it! Here's code for another solution without lookaheads, which has several benefits. 1. It's a bit more general, in case you'd like to capture files with various numbers, 2. It also works for files that have a dot in them, like try.this.gh It also matches a bit faster (61 steps vs 112 for the gh string you supplied), but that's immaterial. Input: "http://www.grasshopper3d.com/forum/attachment/download?id=2985220%3AUploadedFile%3A501843">01.jpg</a> "http://www.grasshopper3d.com/forum/attachment/download?id=2985220%3AUploadedFile%3A506981">SURFACE-DIAGRID-TEST.gh</a> "http://www.grasshopper3d.com/forum/attachment/download?id=88UploadedFile%3A981">AnotherOne.gh</a>' Code: <?php $string = '"http://www.grasshopper3d.com/forum/attachment/download?id=2985220%3AUploadedFile%3A501843">01.jpg</a> "http://www.grasshopper3d.com/forum/attachment/download?id=2985220%3AUploadedFile%3A506981">SURFACE-DIAGRID-TEST.gh</a> "http://www.grasshopper3d.com/forum/attachment/download?id=88UploadedFile%3A981">Another.One.gh</a>'; $pattern = ',(http://www\.grassh[^?]+\?id[^U]+Up[^>]+>(([^.<]*?\.?)*))\.gh,'; $hit = preg_match_all($pattern,$string,$matches,PREG_PATTERN_ORDER); $sz=count($matches[0]); for ($i=0;$i<$sz;$i++) { echo "Match: ".$matches[1][$i]."<br />"; echo "File: ".$matches[2][$i]."<br /><br />"; } ?> Output: Match: http://www.grasshopper3d.com/forum/attachment/download?id=2985220%3AUploadedFile%3A506981">SURFACE-DIAGRID-TEST File: SURFACE-DIAGRID-TEST Match: http://www.grasshopper3d.com/forum/attachment/download?id=88UploadedFile%3A981">Another.One File: Another.One Nothing wrong with aba's solution, just wanting to give you another option. Let us know if these work for you.
  5. And thank you for your courtesy! It makes answering questions that much more pleasurable when the person who asks is as courteous as you. Wishing you a fun weekend.
  6. Fantastic. The manual's ord page also has an example with a utfCharToNumber function that might work better in some cases.
  7. Hi Mcod, Here's a trick I learned a few days ago on another forum to debug a string with "hidden characters". I would try it on some of your input. Input: Hello Mcod Code: <?php $string = 'Hello Mcod'; for($i=0;$i<strlen($string);$i++) echo "{{$string[$i]}}" . ord($string[$i]) . " <br />\n"; ?> Output: {H}72 {e}101 {l}108 {l}108 {o}111 { }32 { }9 {M}77 {c}99 {o}111 {d}100 Let me know if that works for you.
  8. Fantastic. Yes, please let me know if it works on real data / needs tweaking.
  9. Fantastic. I love Pikachu's simple yet efficient idea. Once you've used parse_url, you'll need code to extract what you want from the array. As an alternate solution, in case you'd like to do it with regex, try this code. If I've understood your request, it should do what you want. Input: /reserve-unit/?unit=1000123801&loc=1000000174&transaction=1002731154&complete=1000706731 Code: <?php $pattern=',/([^/]+)/.*?(loc=[^&]+)&[^&]+&([\w]+)=,'; $subject='/reserve-unit/?unit=1000123801&loc=1000000174&transaction=1002731154&complete=1000706731'; preg_match($pattern,$subject,$match); for($i=1;$i<count($match);$i++) // or in this case, 4 instead of count($match) echo $match[$i].'<br />'; ?> Output: reserve-unit loc=1000000174 complete Please let me know if you'd like further details.
  10. Hi Mcod, Try this code, it should do what you want. Input: two spaces. Space+tab here. Three spaces_and_tab. Code: <?php $pattern='~\s{2,}~'; $replacement=' '; $subject='two spaces. Space+tab here. Three spaces_and_tab.'; $s=preg_replace($pattern,$replacement,$subject); echo $s.'<br />'; ?> Output: two spaces. Space+tab here. Three spaces_and_tab. Note that the replacement string is one space. You can make it an empty string if you like. If this is not quite what you want, please supply an example.
  11. Hi Andy! Run this sample code, I think it does what you want. Input: an example: www.p.pl would be done as link, but "www", "www.", "www.s", "www..", "www.s." wouldn\'t Code: <?php $pattern=',(www\.(\w+(?:\.\w+)+)),'; $replacement='<a target=\'_blank\' href="http:\1">\2</a>'; $subject='an example: www.p.pl would be done as link, but "www", "www.", "www.s", "www..", "www.s." wouldn\'t '; $s=preg_replace($pattern,$replacement,$subject); echo htmlentities($s).'<br />'; ?> Output: an example: <a target='_blank' href="http:www.p.pl">p.pl</a> would be done as link, but "www", "www.", "www.s", "www..", "www.s." wouldn't Is this what you're looking for?
  12. Glad to help, bro. Thanks for your friendly message.
  13. Hey Mcod, Try this. Should work with s, sessid, sessionid. $pattern=',php\?\Ks(?:ession|essid)?=[^&]*&,'; $s = 'http://www.domain.com/forumdisplay.php?s=3d496add17bf8a05551109b5693e9489&f=33'; $s = preg_replace($pattern,"",$s); echo $s;
  14. Addendum: If you are creating your pattern programmatically, filling it with text you got from somewhere else, you may not know whether your string contains your delimiter (whether it's an ugly slash or a comma). In that case, preg_quote is your friend. Here's an example. You are building a pattern based on an input string. You don't know what it's there, but you're worried it might have your delimiter (in this case, a comma). $input='pass me the salt, she said'; $subject='LONG_TEXT pass me the salt, she said again LONG_TEXT'; $input=preg_quote($input,','); $pattern=','.$input.'\s*\b\K\w+\b,'; preg_match($pattern,$subject,$match); echo $match[0].'<br />'; Output: again
  15. Hi Mike, You can use pretty much whatever you want as a delimiter. (Not a backslash.) For some odd reason, the forward slash is often used as an example. But it has lots of drawbacks. Whenever you use a delimiter inside a string, you have to escape it. I don't know about you, but I don't find something like: /http:\/\// particularly nice to look at. You'll find that a lot of regex heads have their favorite delimiters. You'll find a lot of #, %, ~, &, @. I like the comma because it is unobtrusive. The expression itself really stands out. And when you use expressions in a number of places besides PHP, you tend to think of the expression without the delimiters. As an example, you wouldn't use delimiters in RegexBuddy.
  16. Hi Mike, Glad it works! First, I was going to mention that if you are using a version of PHP before 5.2.4, it wouldn't work and you would need something like: $pattern=',(?<=")[^"]+,'; Okay, let's look at the pattern: ',"\K[^"]+,' 1. The commas at the front and back are the delimiters. 2. " matches a double quote. 3. \K says "drop what we have matched so far from what we will report as a match (so the engine keeps its position in the string, but it won't report the first quote in the match). 4. [^"]+ says match one or more characters (the + quantifier) that are not a double quote. 5. We don't need to tell the engine to match a double quote, because we know that what comes after "not a double quote" must be a double quote. Note that this is because you know how your string is built. This expression would also replace text that starts with a double quote, but has no closing double quote, such as 'blah "adg' Let me know if you have more questions! Wishing you a fun day
  17. Hi Mike, Would this work for you? It replaces everything between the first pair of double quotes. Input: 'blah "site name" blah' Code: $string='blah "site name" blah'; $pattern=',"\K[^"]+,'; $new='look at me!'; $s=preg_replace($pattern,$new,$string,1); echo $s; Output: blah "look at me!" blah
  18. You're welcome, really glad it helped. Warmest wishes.
  19. yes it's outputting "$tables['settings']" Great. So the expression is working. SELECT * FROM `{settings}` is the result of that echo My echo is "SELECT * FROM `".$tables['settings']."` Maybe a copy-paste problem? The main thing is that the first one works. Starting from there, you can introduce the dot (.) and the grave (`) into the expression that works, in the places where I had them, in order to get around the problem.
  20. Also, I see in the output string the the string continuation dot and the `"grave accent" are missing, so you can change the pattern to this: $s=preg_replace('/"SELECT \* FROM `\{([^}]+)\}`"/', '"SELECT * FROM `".$tables[\'\1\']."`', $subject); Keep the echo and tell me if you get this output: "SELECT * FROM `".$tables['settings']."`
  21. Can you please try an exact copy-paste on my code from the first post, as I don't want to go blind trying to see if there is a difference with yours: $subject='"SELECT * FROM `{settings}`"'; $s=preg_replace('/"SELECT \* FROM `\{([^}]+)\}`"/', '"SELECT * FROM `".$tables[\'\1\']"', $subject); echo $s; Here is the output I get: "SELECT * FROM `".$tables['settings']" Please let me know if you do not get that.
  22. 1. In your code, you have an echo $query; Does it output the query string you expect? In other words, the first question is: Does the regex work according to the specs you specified? You may have other problems (talking to the database), but first, we need to know: does the regex do what you wanted it to do? 2. We prepared a query string ($query) according to your specs using regex, and I have no idea if the syntax of the query itself is correct, I only looked at your specs. But you seem to be executing a different query altogether anyhow. I was expecting to see something like: $result=$db->query($query); Not Execute(a query completely unrelated to $query) (The query method above assumes you have already created a database object, at its simplest form: $db= new PDO ($dsn,$dbUser,$dbPass); You are probably using another database interface.)
  23. How is this different from the first solution I gave you? As requested, among other things, it replaces whatever is in the braces: {settings}, {users} or whatever with $tables['settings'], $tables['users'] or whatever. Please be as clear as possible as it is very hard to troubleshoot regex without extremely clear communication about desired input and output.
  24. If that's literally what you want, no problem, but are you sure you literally want $tables[\1] ??? What does the \1 refer to? (How is it going to work in your SELECT statement?) Anyhow:
×
×
  • 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.