SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    SF
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face How to cut off a fixed "word" in a string

    Ok FOr example, so I have my domain, something like mytestyou.com. I have a few domain that also end with "you" such as Itestyou.com and wetestyou.com

    I want to show the domain names, but without the extentions (such as.com .net whatever and also cut off the "you" part) How would I do that?

    Please help, I am a student, not much experiences.
    Maternity.com Ultimate Source for Expecting Parents and Parents

  2. #2
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $s 'itestyou.org';
    echo 
    preg_replace('/you[.][a-z]{2,4}/i','',$s); 
    this regular expression looks for you, followed by a period (which must be in brackets because period is a special character in a regular expression), followed by 2 to 4 letters in the range of a-z. the replacement is '', which means that any matches will be replaced with nothing. the i at the end makes it case insensitive.

  3. #3
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $word 'you';
    // Remove the part after dot
    $pos strrpos($domain'.');
    $domain substr($domain$pos);
    // Remove the $word ending, if it's present
    if (substr($domain, -strlen($word)) == $word) {
      
    $domain substr($domain0, -strlen($word));


  4. #4
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    SF
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by earl-grey View Post
    PHP Code:
    $word 'you';
    // Remove the part after dot
    $pos strrpos($domain'.');
    $domain substr($domain$pos);
    // Remove the $word ending, if it's present
    if (substr($domain, -strlen($word)) == $word) {
      
    $domain substr($domain0, -strlen($word));


    I am little confused, but I already have a var that removed the extension after the dot, called "domain_remove" , so do I use that or i use var that actually list full domain name?
    Maternity.com Ultimate Source for Expecting Parents and Parents

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    regex

    @longneck I thought an unescaped "." meant "any character" (including a ".")
    AFAIK it should be
    PHP Code:
    echo preg_replace('/you\.[a-z]{2,4}/i','',$s); 
    to be "." specific.

  6. #6
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    I thought an unescaped "." meant "any character"
    A dot within a character class is literal, not wild, hence it matches a dot and nothing else.

  7. #7
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    . is litteral when inside of a character class. it wouldnt really make sense to match any character in a character class

    although yes, i think its more common to forgo the character class and just escape the .

  8. #8
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    character class

    Yes, right you are. I didn't "see" it all by it's lonesome like that.

  9. #9
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher View Post
    i think its more common to forgo the character class and just escape the .
    It makes no sense whatsoever to have a positive character class with only a single character in it. My guess would be the overhead is enormous when compared to just an escaped dot.

  10. #10
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by CrazyMod
    I am little confused, but I already have a var that removed the extension after the dot, called "domain_remove" , so do I use that or i use var that actually list full domain name?
    In that case using this should be enough:
    PHP Code:
    $word 'you'
    // Remove the $word ending, if it's present 
    if (substr($domain, -strlen($word)) == $word) { 
      
    $domain substr($domain0, -strlen($word)); 


  11. #11
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bokehman View Post
    It makes no sense whatsoever to have a positive character class with only a single character in it. My guess would be the overhead is enormous when compared to just an escaped dot.
    i've never really thought about the performance implications of that construct. i find it more difficult to read backslashes than the single character class.

  12. #12
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    SF
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by earl-grey View Post
    In that case using this should be enough:
    PHP Code:
    $word 'you'
    // Remove the $word ending, if it's present 
    if (substr($domain, -strlen($word)) == $word) { 
      
    $domain substr($domain0, -strlen($word)); 

    isnt it suppose to be ?

    PHP Code:
    $word 'you'
    // Remove the $word ending, if it's present 
    if (substr($domain_remove, -strlen($word)) == $word) { 
      
    $domain_remove substr($domain_remove0, -strlen($word)); 

    Maternity.com Ultimate Source for Expecting Parents and Parents

  13. #13
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by CrazyMod View Post
    isnt it suppose to be ?

    PHP Code:
    $word 'you'
    // Remove the $word ending, if it's present 
    if (substr($domain_remove, -strlen($word)) == $word) { 
      
    $domain_remove substr($domain_remove0, -strlen($word)); 

    Yes, I assumed that $domain contains domain name without the part after dot and dot itself.

  14. #14
    SitePoint Enthusiast Salkcin's Avatar
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or you could just split the string on "you" using explode(), ex:

    PHP Code:
    <?php

    $domain 
    "http://www.wetestyou.com";
    list(
    $var) = explode('you'$domain);

    echo 
    $var// Output:  http://www.wetest

    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •