1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Remove specific text from input data

Discussion in 'JavaScript' started by Tuhin1234, Apr 10, 2016.

  1. #1
    I have a script like this

    function convertUrl($originalUrl){
    $tmp1 = substr($originalUrl,0, strrpos($originalUrl,"/"));
    $tmp2 = substr($tmp1,0, strrpos($tmp1,"/"));
    $tmp3 = substr($originalUrl, strrpos($originalUrl,"/"), strlen($originalUrl));
    return $tmp2 . $tmp3;}
    echo convertUrl("siteurl/id124536/05/how_to_make_a_php_site");
    Code (markup):
    The function should remove some specific text from url like:

    siteurl/id124536**/05**/how_to_make_a_php_site
    Now I want, when I input siteurl/id124536**/05**/how_to_make_a_php_site like this url script work otherwise not work.

    [example: when i input siteurl/id124536/how_to_make_a_php_site like this url script not working]
     
    Tuhin1234, Apr 10, 2016 IP
  2. Blizzardofozz

    Blizzardofozz Well-Known Member

    Messages:
    132
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    118
    #2
    This is for php not javascript section.

    What are you trying to do? The result of your function is removing "/05" from the string as I understand but in the example that doesn't work you don't have "/05" and the argument is the result you are trying to achieve so I don't get it.

    Function is not working because the number of slashes is hard coded in the function and if you change this number the function will fail.

    I think next will be better:
    
    function convertUrl($originalUrl){
       $urlArray = explode('/', $originalUrl);
       unset($urlArray[2]);
       return implode('/', $urlArray);
    }
    echo convertUrl("siteurl/id124536/05/how_to_make_a_php_site");
    
    Code (markup):
    But you have to have logic for different number of slashes
    
    function convertUrl($originalUrl){
       $urlArray = explode('/', $originalUrl);
       
        if (count($urlArray) === 4 ) {
             unset($urlArray[2]);
        }  elseif (count($urlArray) === 3) {
            unset what pat of the url you want
        }
     
       return implode('/', $urlArray);
    }
    echo convertUrl("siteurl/id124536/05/how_to_make_a_php_site");
    
    Code (markup):
     
    Blizzardofozz, Apr 11, 2016 IP
  3. Tuhin1234

    Tuhin1234 Active Member

    Messages:
    178
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #3
    Firs
    First code work but 2nd code not work
     
    Tuhin1234, Apr 11, 2016 IP
  4. Tuhin1234

    Tuhin1234 Active Member

    Messages:
    178
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #4
    Please sir help me 2nd code not work....No change of url
     
    Tuhin1234, Apr 11, 2016 IP
  5. Blizzardofozz

    Blizzardofozz Well-Known Member

    Messages:
    132
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    118
    #5
    Second function is not working because is not finished. You had to tell me what is your goal because I don't understand what the function must do. What result you want from the function?

    Is this your goal: siteurl/id124536/how_to_make_a_php_site ? Do you want the end result to be URL with 2 slashes "/" like here?

    Do you know what URLs will be argument of the function? What URLs the function has to change? With how many slashes in the URL? Where is the string you want to remove - always after first slash or always after second slash or the place changes? Is the string you want to remove always the same?
     
    Last edited: Apr 11, 2016
    Blizzardofozz, Apr 11, 2016 IP
  6. Tuhin1234

    Tuhin1234 Active Member

    Messages:
    178
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #6
    [​IMG] http://s29.postimg.org/pnjgi8uzb/still.jpg
     
    Tuhin1234, Apr 11, 2016 IP
  7. Blizzardofozz

    Blizzardofozz Well-Known Member

    Messages:
    132
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    118
    #7
    
    function convertUrl($originalUrl) {
     
       $cleanUrl = str_replace(array('http://www.', 'http://'), '', $originalUrl); // remove protocol
     
       $urlArray = explode('/', $cleanUrl); // make an array from the string
    
      if (count($urlArray) === 4 ) {
         unset($urlArray[2]); // remove the 3d element if the array has 4 elements
      }
    
      return implode('/', $urlArray); //craete string from the array and return it
    }
    
    echo convertUrl("siteurl/id124536/05/how_to_make_a_php_site");
    echo "<br>";
    echo convertUrl("siteurl/id124536/how_to_make_a_php_site");
    echo "<br>";
    echo convertUrl("http://siteurl.com/id124536/05/how_to_make_a_php_site");
    
    Code (markup):
    results:
    siteurl/id124536/how_to_make_a_php_site
    siteurl/id124536/how_to_make_a_php_site
    siteurl.com/id124536/how_to_make_a_php_site

    Is this the result you wanted or I didn't understand the purpose of the function?
     
    Last edited: Apr 12, 2016
    Blizzardofozz, Apr 12, 2016 IP
  8. Tuhin1234

    Tuhin1234 Active Member

    Messages:
    178
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #8
    Remove http://www. from output url .
    [​IMG]
     
    Tuhin1234, Apr 12, 2016 IP
  9. Tuhin1234

    Tuhin1234 Active Member

    Messages:
    178
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #9
    Please sir last time help me ...
     
    Tuhin1234, Apr 12, 2016 IP
  10. Tuhin1234

    Tuhin1234 Active Member

    Messages:
    178
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #10
    Thanks you so much sir .Now my script working fine .
     
    Tuhin1234, Apr 12, 2016 IP