Extracting part of a string? - php

How can I extract 4 from this string?
$string = "Rank_1:1:4";
I'm trying to get pagerank from Googles server, and the last value (4) is the actual pagerank.

Try
$string = "Rank_1:1:4";
$data = explode(':',$string);
echo end($data);
EDIT
as per #MichaelHampton, if they add more fields later, then use as below
$string = "Rank_1:1:4";
$data = explode(':',$string);
echo $data[2];

PHP has so many string function you can use ...
Variables
$find = ":";
$string = "Rank_1:1:4";
Using substr
echo substr($string, strrpos($string, $find) + 1);
Using strrchr
echo ltrim(strrchr($string, $find),$find);

$pattern = '/:\d+$/';
preg_match($pattern, $string, $matches);
$rank = substr($matches[0],1);

Related

How to remove 4th letter in string using PHP?

How to remove 4th letter in string using PHP ?
I use this code.
<?php
$str = "1234567890";
$str2 = mb_substr($str, 4);
echo $str2;
?>
But it's will echo 567890
I want to echo 123567890 remove 4 from string.
How can i do ?
You can try substr_replace for this. Here we are replacing 4 which is at 3rd index.
Try this code snippet here
<?php
$str = "1234567890";
echo substr_replace($str, "", 3,1);
try setting the 3rd index to null
<?php
$str = "1234567890";
$str[3] = null;
echo $str;
try with below sulution:
$str = '1234567890';
$str_arr = str_split($str);
unset($str_arr[3]);
echo implode('', $str_arr);
output:
123567890
There are multiple ways of performing any operations on string variables in php
// can be used for printing purpose
$str = "1234567890";
echo substr($str,0,3).substr($str,4);
// actual replacement of string
$str = "1234567890";
echo substr_replace($str, "", 3,1);

How to remove anything character after the part of link from string?

I have make a try like this:
$string = "localhost/product/-/123456-Ebook-Guitar";
echo $string = substr($string, 0, strpos(strrev($string), "-/(0-9+)")-13);
and the output work :
localhost/product/-/123456 cause this just for above link with 13 character after /-/123456
How to remove all? i try
$string = "localhost/product/-/123456-Ebook-Guitar";
echo $string = substr($string, 0, strpos(strrev($string), "-/(0-9+)")-(.*));
not work and error sintax.
and i try
$string = "localhost/product/-/123456-Ebook-Guitar";
echo $string = substr($string, 0, strpos(strrev($string), "-/(0-9+)")-999);
the output is empty..
Assume there are no number after localhost/product/-/123456, then I will just trim it with below
$string = "localhost/product/-/123456-Ebook-Guitar";
echo rtrim($string, "a..zA..Z-"); // localhost/product/-/123456
Another non-regex version, but require 5.3.0+
$str = "localhost/product/-/123456-Ebook-Guitar-1-pdf/";
echo dirname($str) . "/" . strstr(basename($str), "-", true); //localhost/product/-/123456
Heres a more flexibility way but involve in regex
$string = "localhost/product/-/123456-Ebook-Guitar";
echo preg_replace("/^([^?]*-\/\d+)([^?]*)/", "$1", $string);
// localhost/product/-/123456
$string = "localhost/product/-/123456-Ebook-Guitar-1-pdf/";
echo preg_replace("/^([^?]*-\/\d+)([^?]*)/", "$1", $string);
// localhost/product/-/123456
This should match capture everything up to the number and remove everything afterward
regex101: localhost/product/-/123456-Ebook-Guitar
regex101: localhost/product/-/123456-Ebook-Guitar-1-pdf/
Not a one-liner, but this will do the trick:
$string = "localhost/product/-/123456-Ebook-Guitar";
// explode by "/"
$array1 = explode('/', $string);
// take the last element
$last = array_pop($array1);
// explode by "-"
$array2 = explode('-', $last);
// and finally, concatenate only what we want
$result = implode('/', $array1) . '/' . $array2[0];
// $result ---> "localhost/product/-/123456"

Extract specific word from a string

It might seem easy to do but I have trouble extracting this string. I have a string that has # tags in it and I'm trying to pull the tags maps/place/Residences+Jardins+de+Majorelle/#33.536759,-7.613825,17z/data=!3m1!4b1!4m2!3m1!1s0xda62d6053931323:0x2f978f4d1aabb1aa
And here is what I want to extract 33.536759,-7.613825,17z :
$var = preg_match_all("/#(\w*)/",$path,$query);
Any way I can do this? Much appreciated.
Change your regex to this one: /#([\w\d\.\,-]*)/.
This will return the string beginning with #.
$string = 'maps/place/Residences+Jardins+de+Majorelle/#33.536759,-7.613825,17z/data=!3m1!4b1!4m2!3m1!1s0xda62d6053931323:0x2f978f4d1aabb1aa';
$string = explode('/',$string);
//$coordinates = substr($string[3], 1);
//print_r($coordinates);
foreach ($string as $substring) {
if (substr( $substring, 0, 1 ) === "#") {
$coordinates = $substring;
}
}
echo $coordinates;
This is working for me:
$path = "maps/place/Residences+Jardins+de+Majorelle/#33.536759,-7.613825,17z/data=!3m1!4b1!4m2!3m1!1s0xda62d6053931323:0x2f978f4d1aabb1aa";
$var = preg_match_all("/#([^\/]+)/",$path,$query);
print $query[1][0];
A regex would do.
/#(-*\d+\.\d+),(-*\d\.\d+,\d+z*)/
If there is only one # and the string ends with / you can use the following code:
//String
$string = 'maps/place/Residences+Jardins+de+Majorelle/#33.536759,-7.613825,17z/data=!3m1!4b1!4m2!3m1!1s0xda62d6053931323:0x2f978f4d1aabb1aa';
//Save string after the first #
$coordinates = strstr($string, '#');
//Remove #
$coordinates = str_replace('#', '', $coordinates);
//Separate string on every /
$coordinates = explode('/', $coordinates );
//Save first part
$coordinates = $coordinates[0];
//Do what you want
echo $coordinates;
do like this
$re = '/#((.*?),-(.*?),)/mi';
$str = 'maps/place/Residences+Jardins+de+Majorelle/#33.536759,-7.613825,17z/data=!3m1!4b1!4m2!3m1!1s0xda62d6053931323:0x2f978f4d1aabb1aa';
preg_match_all($re, $str, $matches);
echo $matches[2][0].'<br>';
echo $matches[3][0];
output
33.536759
7.613825

Using preg_replace to get id

how to get id url with preg_replace.
this is the link:
http://www.DDDD.com.br/photo/5b87f8eaa7c20f79c3257eb3ec0a35e0/id how do I get the id? in the case would be: 5b87f8eaa7c20f79c3257eb3ec0a35e0
In this case I recommend not to use preg_match (preg_replace would be used to replace something.
Simply use
$array = explode('/',$_SERVER['REQUEST_URI']);
$id = $array[1];
If you must use preg_match:
$array = array();
preg_match('#^/photo/([0-9a-f]{32})/id$#',$_SERVER['REQUEST_URI'],$array);
$id = $array[1];
You can do this easily using strripos to find the last / in the URL.
$url = $_SERVER['REQUEST_URI'];
if (($pos = strripos($url, '/')) !== false) {
$id = substr($url, $pos + 1);
}
else {
trigger_error('You must supply a valid photo ID');
}
If you would like to just extract that id string, you can use:
$id_url = "http://www.DDDD.com.br/photo/5b87f8eaa7c20f79c3257eb3ec0a35e0/id";
$pattern = "/photo\/([a-zA-Z0-9]*)/";
preg_match($pattern, $id_url, $output_array);
echo $output_array[1];
Or, to make the replacement:
$id_url = "http://www.DDDD.com.br/photo/5b87f8eaa7c20f79c3257eb3ec0a35e0/id";
$pattern = "/photo\/([a-zA-Z0-9]*)/";
$replacement = "your replacement";
$replaced_url = preg_replace($pattern, $replacement, $id_url);
echo $replaced_url;
PHP Live Regex - a useful tool for testing your patterns

Removing Certain Text from String in PHP

I am trying to trim a string in PHP so that I can only get certain text from the String.
I have an email stored to a String for instance some_name#somedomain.com .
How can I remove the text after the '#' so that I would only 'some_name'?
In PHP you can do :
$string = 'some_name#somedomain.com';
$res = explode('#', $string);
echo $res[0];
Or you can use regexp, string functions in php ... etc
You should know both ways to do this:
substr
$mail = "some_name#somedomain.com";
echo substr($mail, 0, strpos($mail, '#') );
explode
list($name, $domain) = explode('#', $mail);
echo $name;
If you don't need the $domain you can skip it:
list($name) = explode('#', $mail);
More about list.
Demo: http://ideone.com/lbvQF
$str = 'some_name#somedomain.com';
$strpos = strpos($str, "#");
echo $email = substr($str, 0,$strpos);
you can try this to get string before #
Try This
$str1 = "Hello World";
echo trim($str1,"World");
You could try split using regex and the # symbol. This will return two Strings which you can then use just to acquire the 'some_name'.
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html
String s = "some_name#somedomain.com";
String name = s.substring(0,s.indexOf("#");

Categories