php how to get a string in a specific place - php

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

Related

How to extract only parameter value from URL using ONLY Regular Expressions

Extract the value of the u2 parameter from this URL using a regular expression. http://www.example.com?u1=US&u2=HA853&u3=HPA
<?php
$subject="http://www.example.com?u1=US&u2=HA853&u3=HPA"; //my url
$pattern='/u2=[0-9A-Za-z]*/'; //R.E that url value is only digit/Alphabet
preg_match($pattern,$subject,$match);
print_r($match[0]);
?>
Output:-
u2=HA853
How can i retrieve only HA853?
The 0 group is everything that the regex matched so either use \K to ignore the previous matches of the regex,
$subject="http://www.example.com?u1=US&u2=HA853&u3=HPA"; //my url
$pattern='/u2=\K[0-9A-Za-z]*/'; //R.E that url value is only digit/Alphabet
preg_match($pattern,$subject,$match);
print_r($match[0]);
or use a second capture group:
...
$pattern='/u2=([0-9A-Za-z]*)/'; //R.E that url value is only digit/Alphabet
...
print_r($match[1]);
Why you'd need to do that though is unclear to me, http://php.net/manual/en/function.parse-str.php, seems like a simpler approach.
$subject="http://www.example.com?u1=US&u2=HA853&u3=HPA";
parse_str($subject, $output);
echo $output['u2'];
Demo: https://3v4l.org/gR4cb
Other way is to use parse_url,http://php.net/manual/en/function.parse-url.php
$subject="http://www.example.com?u1=US&u2=HA853&u3=HPA";
$query_string = parse_url($subject, PHP_URL_QUERY); // get query string
$parameters = explode('&', $query_string); //Explode with &
$array = array(); // define an empty array
foreach($parameters as $val)
{
$param= explode('=', $val);
$array[$param[0]] = $param[1];
}
echo $array['u2']; // outputs HA853
print_r($array);
Array
(
[u1] => US
[u2] => HA853
[u3] => HPA
)

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

php getting substring from a string

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)

Categories