Asked  7 Months ago    Answers:  5   Viewed   40 times
$string = "Hello World Again".
echo strrchr($string , ' '); // Gets ' Again'

Now I want to get "Hello World" from the $string [The substring before the last occurrence of a space ' ' ]. How do I get it??

 Answers

51

This is kind of a cheap way to do it, but you could split, pop, and then join to get it done:

$string = 'Hello World Again';
$string = explode(' ', $string);
array_pop($string);
$string = implode(' ', $string);
Wednesday, March 31, 2021
 
gMale
answered 7 Months ago
31

You should use the tool that is designed for this type of job, parse_url

url.php

<?php

$urls = array('http://example.com/foo?u=ben',
                'http://example.com/foo/bar/?u=ben',
                'http://example.com/foo/bar/baz?u=ben',
                'https://foo.example.com/foo/bar/baz?u=ben',
            );


function clean_url($url) {
    $parts = parse_url($url);
    return $parts['scheme'] . '://' . $parts['host'] . $parts['path'];
}

foreach ($urls as $url) {
    echo clean_url($url) . "n";
}

Example:

ยท> php url.php                                                                                                 
http://example.com/foo
http://example.com/foo/bar/
http://example.com/foo/bar/baz
https://foo.example.com/foo/bar/baz
Wednesday, March 31, 2021
 
Nickool
answered 7 Months ago
86

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario: http://confluence.jetbrains.com/display/PhpStorm/PHPUnit+Installation+via+Composer+in+PhpStorm

Related usability ticket: http://youtrack.jetbrains.com/issue/WI-18388

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
 
ojrac
answered 7 Months ago
79

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
 
Nate
answered 5 Months ago
74

You probably want something like /(.*?):(.*)/. The ? after the * will make it "non-greedy", so it will consume as little text as possible that way. I think that will work for your situation. By default, * is "greedy", and tries to match as many repetitions as it can.

Edit: See here for more about matching repetition using the * and + operators.

Saturday, May 29, 2021
 
JakeGR
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :