php getting substring from a string - php

I have the next URL: http://domen.com/aaa/bbb/ccc.
How can I get the string after http://domen.com/?
Thanks a lot.

$sub = substr($string, 0, 10);
But if you actually want to parse the URL (that is, you want it to work with all URLs), use parse_url. For "http://domen.com/aaa/bbb/ccc", it would give you an array like this:
Array
(
[scheme] => http
[host] => domen.com
[user] =>
[pass] =>
[path] => /aaa/bbb/ccc
[query] =>
[fragment] =>
)
You could then compile this into the original url (to get http://domen.com/):
$output = $url['scheme'] . "://" . $url['host'] . $url['path'];
assuming $url contains the parse_url results.

You can use PHP's split.
Your code will be something like:
$s = "http://domen.com/aaa/bbb/ccc";
$vals = split("http://domen.com/", $s);
// $v will contain aaa/bbb/ccc
$v = $vals[1];

parse_url()

http://php.net/manual/en/function.parse-url.php Might be the way to go.

If you simply want the string and the "http://domen.com/" part is fixed:
$url = 'http://domen.com/aaa/bbb/ccc';
$str = str_replace('http://domen.com/','',$url);

Use the regex for example like the function preg_replace

Try this:
preg_replace('/^.*?\w\//', '', $url)

Related

php how to get a string in a specific place

I have this string
http://myipaddress:myport/mycompanyname/morethings?lovelyparameter
I want to take the word mycompanyname
any help?
I tried this:
$indexName = preg_match("http://p+:p+/","http://myipaddress:myport/mycompanyname/morethings?lovelyparameter" );
but I got this error:
preg_match(): Delimiter must not be alphanumeric or backslash
In case you don't want the preg functions, and something else from the url, you can use parse_url(). It would look like this:
$a = 'http://myipaddress:8080/mycompanyname/morethings?lovelyparameter';
$b = parse_url($a);
print_r($b);
Output:
Array
(
[scheme] => http
[host] => myipaddress
[port] => 8080
[path] => /mycompanyname/morethings
[query] => lovelyparameter
)
That way, just use something like:
$path = $b['path'];
$foo = explode('/', $path)[1];
echo $foo;
Output:
mycompanyname
Side notes:
This code won't check for malformed url, so you should do some check of your own.
If you test the url with a port number as string (as you have in the question), it won't work.
It could be done in one line:
$url = 'http://myipaddress:8080/mycompanyname/morethings?lovelyparameter';
echo explode('/', parse_url($url, PHP_URL_PATH))[1];
Output:
mycompanyname
You can use explode as
$abc = 'http://myipaddress:myport/mycompanyname/morethings?lovelyparameter';
$a = explode('/', $abc);
echo '<pre>';
print_r($a[3]);
echo '</pre>';
The explode breaks the strings into parts and returns an array of strings so you can check in array too for mycompanyname..
For the records, you were missing appropriate delimiters. A regex solution would be:
https?://.+?/(?P<company>[^/]+)/
In PHP this would be:
$regex = '~https?://.+?/(?P<company>[^/]+)/~';
$url = 'http://myipaddress:8080/mycompanyname/morethings?lovelyparameter';
preg_match($regex, $url, $match);
echo $match["company"];
// mycompanyname

How can I get second to last segment in URL and skip all GET queries with PHP?

I have a url - http://example.com/accounts/users/list?showlist=true&id=4 and I need to get the users segment out of this. How can it be done? I know there's substr command, but not quite sure how to use it. Can you please show me an example?
Thanks you.
This should work for you:
(Here I first get the dirname() of the $url, so that the last part is users, then I can grab that with taken the basename() from it)
$url = "http://example.com/accounts/users/list?showlist=true&id=4";
echo basename(dirname($url));
output:
users
If you assume that you always want to get the second value, you first use parse_url to get the path. Then you explode the path by "/" and filter the empty values. Your requested value will be in the second array value.
If you don't expect the value to be always in the second value, please specifiy your question further.
See:
$url = 'http://example.com/accounts/users/list?showlist=true&id=4';
$parts = parse_url($url);
$fragments = explode('/', $parts['path']);
$fragments = array_filter($fragments);
$value = $fragments[2];
echo $value;
Parsing URL with PHP
Explode - one of the usefullnest methods ;)
And it goes this way:
$url = "http://example.com/accounts/users/list?showlist=true&id=4";
$parsed = parse_url($url); // parsing
$parsed['path'] = explode('/', $parsed['path']); // exploding path
print_r($parsed);
This bit of code will provide you with an array looking this way:
[scheme] => http
[host] => example.com
[path] => Array
(
[0] =>
[1] => accounts
[2] => users
[3] => list
)
[query] => showlist=true&id=4
Rest is very similar to JS ;)
echo 'before last: ' . $parsed['path'][count($parsed['path'])-2];
will produce:
before last: users

Split string using regular expression in php

I'm beginner in php and I have string like this:
$test = http://localhost/biochem/wp-content/uploads//godzilla-article2.jpghttp://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg
And I want to split string to array like this:
Array(
[0] => http://localhost/biochem/wp-content/uploads//godzilla-article2.jpg
[1] => http://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg
)
What should I do?
$test = 'http://localhost/biochem/wp-content/uploads//godzilla-article2.jpghttp://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg';
$testurls = explode('http://',$test);
foreach ($testurls as $testurl) {
if (strlen($testurl)) // because the first item in the array is an empty string
$urls[] = 'http://'. $testurl;
}
print_r($urls);
You asked for a regex solution, so here you go...
$test = "http://localhost/biochem/wp-content/uploads//godzilla-article2.jpghttp://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg";
preg_match_all('/(http:\/\/.+?\.jpg)/',$test,$matches);
print_r($matches[0]);
The expression looks for parts of the string the start with http:// and end with .jpg, with anything in between. This splits your string exactly as requested.
output:
Array
(
[0] => http://localhost/biochem/wp-content/uploads//godzilla-article2.jpg
[1] => http://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg
)
you can split them if they are always like this vith substr() function reference: http://php.net/manual/en/function.substr.php but if they are dynamic in lenght. you need to get a ; or any other sign that is not likely to be used there before 2nd "http://" and then use explode function reference: http://php.net/manual/en/function.explode.php
$string = "http://something.com/;http://something2.com"; $a = explode(";",$string);
Try the following:
<?php
$temp = explode('http://', $test);
foreach($temp as $url) {
$urls[] = 'http://' . $url;
}
print_r($urls);
?>
$test = 'http://localhost/biochem/wp-content/uploads//godzilla-article2.jpghttp://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jp';
array_slice(
array_map(
function($item) { return "http://" . $item;},
explode("http://", $test)),
1);
For answering this question by regular expression I think you want something like this:
$test = "http://localhost/biochem/wp-content/uploads//godzilla-article2.jpghttp://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg";
$keywords = preg_split("/.http:\/\//",$test);
print_r($keywords);
It returns exactly something you need:
Array
(
[0] => http://localhost/biochem/wp-content/uploads//godzilla-article2.jp
[1] => localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg
)

Problems with PHP explode

I'm using this code:
$imageurl = "http://siteadress/sites/default/files/bjorn_4.jpg";
$pieces = explode('/', $imageurl);
print_r($pieces);
to split up an url.
The print_r gives me this result =
Array ( [0] => http://siteadress/sites/default/files/bjorn_4.jpg )
Shouldn't it split up the URL after each /? So it will be Array ( [0] => http:/ [1] => / [2] => siteadresses or something like that?
I think you should try :
$imageurl = [node:field_banner_image];
Because with the quotes explode will think that the string is [node:field_banner_image] and not the string inside.
like Edouard Moinard said
$imageurl = [node:field_banner_image];
$pieces = explode('/', $imageurl);
print_r($pieces);
this should work
Try to save your Array[0] element to any variable and split that variable like:
$image=Array[0];
$pieces = explode('/', $image);
print_r($pieces);
I'm unfamiliar with Drupal but a quick read of the documentation gave me this:
http://api.drupal.org/api/drupal/core!includes!token.inc/function/token_replace/8
token_replace()
Replaces all tokens in a given string with appropriate values.
Hopefully that helps
Just giving a try
Check the view source of the page, check if you are getting %2F or "/"
Secondly, check with explode('/', << the field value >>)

How to extract parts using regular expression in PHP?

For example you have the following string:
$text = "word1:text1#atpart/foo/do/myfood$textfinal";
The function will work like:
$parts = array();
extract( $regular_exp, $text, $parts );
In the parts array we will get this:
$parts[0] = "word1";
$parts[1] = "text1";
$parts[2] = "atpart";
$parts[3] = "/foo/do/myfood";
$parts[4] = "textfinal";
Thanks!
This may not be what you are after, but the format you show looks almost like a URL with a username:password#domain authentication in front. If you can get the last $ to be served as a ?, it might be an idea to use parse_url() to parse it.
$string = "word1:text1#atpart/foo/do/myfood?textfinal"; // notice the ?
$string = "none://".$string; // We need to add a protocol for this to work
print_r (parse_url($string));
Result:
Array (
[scheme] => none
[host] => atpart
[user] => word1
[pass] => text1
[path] => /foo/do/myfood
[query] => textfinal )
the advantage of this would be that it's pretty flexible if one or more parts can be missing in the incoming data. If that's not an issue, a regex may be more convenient.
try
$parts = preg_split('/[:#\$]+/', $text);
Without more details, this matches the proposed example:
preg_match('#(.*?):(.*?)#(.*?)(/.*?)\$(.*)#', $text, $parts);
note that you will get the parts starting at index 1 instead of 0.
$delims=':#$';
$word = strtok('word1:text1#atpart/foo/do/myfood$textfinal',$delims);
while ( $word!==false ) {
foreach( explode('/',$word,2) as $tmp){
$words[]=$tmp;
}
$word = strtok($delims);
}
var_dump($words);
On one hand this is probably overkill. On the other hand, this may be more flexible depending on how different the string can be.
Demo: http://codepad.org/vy5b9yX7
Docs: http://php.net/strtok

Categories