http://www.mywebsite/product-tag/animal/
How to I echo animal without the backslash and in big caps like this:
ANIMAL
$link = 'http://www.mywebsite/product-tag/animal/';
$parts = explode( '/', $link );
echo(strtoupper($parts[4]));
If you want auto searching, then you need to use preg_match.
<?php
$string = 'http://www.mywebsite/product-tag/animal/';
$urlparts = explode("/", $string);
$animal = ($string[strlen($string)-1] == '/'? $urlparts[count($urlparts)-2] : end($urlparts));
echo strtoupper($animal);
?>
actually I just figured it out
$r = $_SERVER['REQUEST_URI'];
$r = explode('/', $r);
$r = array_filter($r);
$r = array_merge($r, array());
$endofurl = $r[1];
echo $endofurl;
Related
I have variables as follow in PHP
steve_s_baue
marine_camp_se_bell
mike_wane
I want to only keep the first and last part:
steve_baue
marine_bell
mike_wane
I tried to use trim but got stuck.
EDIT: Here is what I tried to far
$row = $pre_results[$i];
$name = $row -> name;
$text = preg_replace('~[^\pL\d]+~u', '_', $name);
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
$text = preg_replace('~[^-\w]+~', '', $text);
$text = trim($text, '_');
$text = preg_replace('~-+~', '_', $text);
$text = strtolower($text);
Any suggestions?
function firstlast($var,$seperator) {
$varr = explode($seperator, $var);
$first = current($varr);
$last = end($varr);
return $first.'_'.$last;
}
$seperator='_';
$old = 'marine_camp_se_bell';
$new = firstlast($old, $seperator);
echo $new;
If you put original strings in an array, you can run array_walk(); with this function
You can check this:
http://nimb.ws/dVHzZJ and
http://nimb.ws/ENgTCm
<?php
$a = "steve_s_baue";
$temp=explode("_",$a);
$arr=array($temp[0],$temp[2]);
print_r(implode("_",$arr));
?>
Do it with explode function, I made a basic example working
<?php
$name = "marine_camp_se_bell";
$var = explode("_",$name);
print_r (explode("_",$name));
//than print as array
echo "</br>";
echo $var[0];
echo "</br>";
echo $var[3];
?>
$name = "john_dont_do_some_doe";
$array = explode("_", $name);
$last = count($array) - 1;
echo $array[0]."_". $array[$last]; //shows: john_doe
*not tested, but should do the job
EDIT: More clear: I want to echo $data as an array.
I have this:
$lic = $_GET['lic'];
$locationtodb = '../../files/';
$licenses = $locationtodb.'filea.txt';
$SearchLicense = $lic;
$pattern = preg_quote($SearchLicense, '/');
$pattern = "/^.*$pattern.*\$/m";
preg_match_all($pattern, $licenses, $matches);
$wholeLine = implode("\n", $matches[0]);
$data = explode(":", $wholeLine);
I can use somethings like this to replace strings:
$CurrentLine = $lic.':'.'active'.':'.$data[2];
$NewLine = $lic.':'.'suspended'.':'.$data[2];
$new_contents = file_get_contents($licenses);
$new_contents = str_replace($CurrentLine,$NewLine,$new_contents);
file_put_contents($licenses,$new_contents);
And it does work !
But if I typed something like:
echo $data[2];
It give nothing ...
Why?
Thanks.
Solution discovered!
$licenses = $locationtodb.'filea.txt';
Must be:
$licenses = file_get_contents($locationtodb.'filea.txt');
I have URLs like this:
http://www.mywebsite.com/carmake/ABCDEFG/123456789
http://www.mywebsite.com/carmake/AAABBBC/124532532
http://www.mywebsite.com/carmake/BNDFKNV/463634213
and I want to change them to this:
http://www.mywebsite.com/carmake/parts/123456789
http://www.mywebsite.com/carmake/parts/124532532
http://www.mywebsite.com/carmake/parts/463634213
How can I change the text between the last to slashes to parts in functions.php
https://regex101.com/r/sH1wA1/1
<?php
$string = 'http://www.mywebsite.com/carmake/ABCDEFG/123456789';
$pattern = '/(.*\/).*\/([^\/]*$)/';
$replacement = '${1}parts/${2}';
echo preg_replace($pattern, $replacement, $string);
?>
Try this:
<?php
$url = "http://www.mywebsite.com/carmake/ABCDEFG/123456789";
$parts = parse_url($url);
$path = $parts['path'];
$pos = strpos($path, '/', 9);
$sub = substr($path, 9, $pos - 9);
$url = str_replace($sub, 'parts', $url);
Split to segments, change and collect back
$a = 'http://www.mywebsite.com/carmake/BNDFKNV/463634213';
$to = 'parts';
$s = explode('/', $a);
$s[count($s)-2] = $to;
echo implode('/', $s);
How to convert this 19,500 string to number. If I do this
<?php
$val = "19,500";
$number = explode("," , $val);
$newnumber = $number[0].$number[1];
?>
But I don't think this is correct way.
LIVE DEMO
You can replace the string ',' to '' and then convert into integer by int
<?php
$number = "19,500";
$updatedNumber = str_replace(',','',$number);
echo (int)$updatedNumber ;
NOTE: int is better to use than intval function.
It reduces overhead of calling the function in terms of Speed.
http://objectmix.com/php/493962-intval-vs-int.html
Try this:
<?php
$val = "19,500";
$val = str_replace(',', '.', $val);
$number = (float)$val;
?>
UPDATED:
if comma comes out as a thousands-separator then:
<?php
$val = "19,500";
$val = str_replace(',', '', $val);
$number = (int)$val;
?>
you can simply replace ',' with ''
$newnumber = str_replace(',', '', $val);
the best way is:
<?php
$val = "19,500";
$tmp = str_replace(',', '', $val);
$newnumber = (float)$tmp;
?>
I think you just want to remove the ,:
<?php
$val = "19,500";
$val = strtr($val, array(',' => ''));
$number = intval($val);
var_dump($number);
use strtr() instead of str_replace() in case of multiple ,
When I see your code it's just sting string(5) "19500"
<?php
$val = "19,500";
$number = explode("," , $val);
$newnumber = $number[0].$number[1];
var_dump($newnumber);
?>
so you can convert to integer like the following
<?php
$val = "19,500";
$number = explode("," , $val);
$newnumber = $number[0].$number[1];
$realinteger = (int)($newnumber);
var_dump($realinteger);
?>
So the result will be int(19500)
Just try this code
$val = "19,500";
$newnumber = intval(str_replace(',', '', str_replace('.', '', $val))); // output : 19500
$val = "19,500.25";
$newnumber = intval(str_replace(',', '', str_replace('.', '', $val))); // output : 1950025
you can edit delimiter that want to replace with balnk string :)
Or you can try this
$newnumber = preg_match("/[^0-9,. -]/", $val) ? 0 : preg_replace("/[^0-9.-]/", "",$val);
just try one. simple as that :)
<?php
$val = "19,500";
$val = str_replace(',','', $val);
$newnumber = number_format($val,2);
?>
OUTPUT:
19,500.00
If you want to have no decimal you can change the 2 in 0. Like this.
OUTPUT:
19,500
$title = '228-example-of-the-title'
I need to convert the string to:
Example Of The Title
How would I do that?
A one-liner,
$title = '228-example-of-the-title';
ucwords(implode(' ', array_slice(explode('-', $title), 1)));
This splits the string on dashes (explode(token, input)),
minus the first element (array_slice(array, offset))
joins the resulting set back up with spaces (implode(glue, array)),
and finally capitalises each word (thanks salathe).
$title = '228-example-of-the-title'
$start_pos = strpos($title, '-');
$friendly_title = str_replace('-', ' ', substr($title, $start_pos + 1));
You can do this using the following code
$title = '228-example-of-the-title';
$parts = explode('-',$title);
array_shift($parts);
$title = implode(' ',$parts);
functions used: explode implode and array_shift
$pieces = explode("-", $title);
$result = "";
for ($i = 1; $i < count(pieces); $i++) {
$result = $result . ucFirst($pieces[$i]);
}
$toArray = explode("-",$title);
$cleanArray = array_shift($toArray);
$finalString = implode(' ' , $cleanArray);
// echo ucwords($finalStirng);
Use explode() to split the "-" and put the string in an array
$title_array = explode("-",$title);
$new_string = "";
for($i=1; $i<count($title_array); $i++)
{
$new_string .= $title_array[$i]." ";
}
echo $new_string;