Jump to content

regular expression to trim a string


jeff5656

Recommended Posts

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!

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.  :-)

Link to comment
Share on other sites

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=
        
        ?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

"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). 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.