jeff5656 Posted April 28, 2011 Share Posted April 28, 2011 Sorry, I tried to understand the regular expressions but can't figure out the following. How do I take this string /direct1/showtable.php and make it showtable.php. In other words strip out the /direct1/ Once I see how this is done I can apply it to other examples. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/234986-regular-expression-to-trim-a-string/ Share on other sites More sharing options...
Muddy_Funster Posted April 28, 2011 Share Posted April 28, 2011 you aren't looking for trim - that's for removing whitespace - you want a substing - have a look here : http://php.net/manual/en/function.substr.php Quote Link to comment https://forums.phpfreaks.com/topic/234986-regular-expression-to-trim-a-string/#findComment-1207635 Share on other sites More sharing options...
jeff5656 Posted April 28, 2011 Author Share Posted April 28, 2011 Well the problem with that function (if I understand the link you gave me - forgive me if I am wrong) is that you need to know how many characters there are. The subdirectory name changes, so that won't work. it could be /direct1/hello.php or /example999/hello.php I just want in any of these cases to arrive at hello.php. I was under the impression that you use regular expression to do this. Quote Link to comment https://forums.phpfreaks.com/topic/234986-regular-expression-to-trim-a-string/#findComment-1207640 Share on other sites More sharing options...
cssfreakie Posted April 28, 2011 Share Posted April 28, 2011 is this maybe something you could use? <?php echo basename('/direct1/showtable.php'); //basename() ftw :-) ?> Quote Link to comment https://forums.phpfreaks.com/topic/234986-regular-expression-to-trim-a-string/#findComment-1207642 Share on other sites More sharing options...
jeff5656 Posted April 28, 2011 Author Share Posted April 28, 2011 That works for this particular example of a directory, but doesn't do it with reg expressions so I guess I will have to learn that another day :-) Looks like based on no response (and a link to substr tutorial), it looks like *others* have difficulties with regular expression too. :-) Quote Link to comment https://forums.phpfreaks.com/topic/234986-regular-expression-to-trim-a-string/#findComment-1207666 Share on other sites More sharing options...
cssfreakie Posted April 28, 2011 Share Posted April 28, 2011 well i am not sure what you want than, but if i read it well, you want the last part of the string. But it seems you want something different because basename() does exactly that! As far as implicit reference to knowledge of others with regex. If you want help on a particular regex, why you post this in the php forum and not in the regex forum? Now run this code and if that is what you wanted you can be happy someone takes time here to respond to you. And if not rephrase what you want. <?php $myarray = array( "http://rtfm.org/php/help.php", "rtfm.org/php/help.php", "http://omg.wtf.bbq.rtfm.org/php/help.php", "http://why.you.want.regex.if not.needed.org/php/help.php" ); $output = array_map(basename, $myarray); var_dump($output); //this outputs: //0 => string 'help.php' (length= //1 => string 'help.php' (length= //2 => string 'help.php' (length= //3 => string 'help.php' (length= ?> Quote Link to comment https://forums.phpfreaks.com/topic/234986-regular-expression-to-trim-a-string/#findComment-1207677 Share on other sites More sharing options...
Pikachu2000 Posted April 28, 2011 Share Posted April 28, 2011 Based on "no response" using a regex pattern, it's more likely that A) a pattern isn't necessary for what you're doing (and if a pattern isn't necessary you should avoid using one anyhow), or B) you've failed to provide an adequate explanation of what you really need, or why you need it. Quote Link to comment https://forums.phpfreaks.com/topic/234986-regular-expression-to-trim-a-string/#findComment-1207680 Share on other sites More sharing options...
salathe Posted April 28, 2011 Share Posted April 28, 2011 preg_match('#[^/]+$#', '/example999/hello.php', $match); echo $match[0]; This looks for all of the non-slash characters at the end of the string. You'll use repetition, a character class and an anchor. Another option would be to use preg_replace to remove the part that you don't need (compared with matching the part that you do need). Quote Link to comment https://forums.phpfreaks.com/topic/234986-regular-expression-to-trim-a-string/#findComment-1207698 Share on other sites More sharing options...
jeff5656 Posted April 28, 2011 Author Share Posted April 28, 2011 "basename() does exactly that!" Obviously basename only works if it's a subdirectory, so I just wanted to know how you would solve this problem more generically. Iended up using basename solution because it was a directory, but for instance how would you use regular expression to exclude everything between the characters / and /. Or maybe the / is confusing. How would you convert this: **hello##goodbye to this: goodbye. Wouldn't you use regular expression for this (and word length varies so you can't use substr) . Also, as someone alluded to I never meant to question people's knowledge here! This is the forum I come to for answers. You guys are *brilliant* and I am constantly amazed at how quick the solutions come back (sometimes in under a minute). Quote Link to comment https://forums.phpfreaks.com/topic/234986-regular-expression-to-trim-a-string/#findComment-1207733 Share on other sites More sharing options...
cssfreakie Posted April 28, 2011 Share Posted April 28, 2011 problem here is a bit that your pattern is not clear. (at least for me that is) in your first question you ask for: /direct1/showtable.php and make it showtable.php. In other words strip out the /direct1/ So people come up with something for that next thing you say you mean something different (which we had to guess I guess). Depending on your specific situation you choose the tools. So if you have a more general pattern, with say 5 example strings and how you want to have the outcome. we might help in a more precise manner. So i didn't meant to be rude, but with the description you gave, that was for me the most logical solution without bringing in the power of regex, since it probably requires more computer thinking than needed. Quote Link to comment https://forums.phpfreaks.com/topic/234986-regular-expression-to-trim-a-string/#findComment-1207760 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.