Add string at the end position of particular text in a string - php

I need to append a text at the end position of the search term.
$baseStr = 'www.abc.com/cdf/?x=10';
$searchStr = 'www.abc.com/';
$insertStr = 'xxx/';
I need to insert $insertStr after 'www.abc.com/' in $baseStr. I get only the start position using strpos.
Expected result:
$resultStr = 'www.abc.com/xxx/cdf/?x=10';
Edit:
Is it possible to find the end position of the search string and solve this?

You can just replace $searchStr with $searchStr plus $insertStr
$baseStr = 'www.abc.com/cdf/?x=10';
$searchStr = 'www.abc.com/';
$insertStr = 'xxx/';
$resultStr = str_replace($searchStr, $searchStr.$insertStr, $baseStr);
echo $resultStr;
gives
www.abc.com/xxx/cdf/?x=10

Use preg_replace here:
$baseStr = 'www.abc.com/cdf/?x=10';
$searchStr = 'www.abc.com/';
$insertStr = 'xxx/';
$a = preg_replace('#'.$searchStr.'#', $searchStr.$insertStr, $baseStr);
echo '<pre>';
print_r($a);
//Output: www.abc.com/xxx/cdf/?x=10
Or you can use str_replace:
$baseStr = 'www.abc.com/cdf/?x=10';
$searchStr = 'www.abc.com/';
$insertStr = 'xxx/';
$a = str_replace($searchStr, $searchStr.$insertStr, $baseStr);
echo '<pre>';
print_r($a);
//Output: www.abc.com/xxx/cdf/?x=10

You can do something like this,
echo str_replace($searchStr, $searchStr.$insertStr,$baseStr);
Just replace your string it will search and replace for you.
Demo.

Related

What is the best approach to extract a tag

I have this elements where I need to extract this {{search_tag}} and replace by a value
https://www.toto.com/search/10/{{search_tag}}.html#_his_
I tried this but I don't if it's the good way, does'nt work.
$words = explode('{{search_tag}} ',$website_url[$n]);
$exists_at = array_search($seach,$words);
if ($exists_at){
echo "Found at ".$exists_at." key in the \$word array";
}
You can use str_replace
$str = 'https://www.toto.com/search/10/{{search_tag}}.html#_his_';
$val = 'NEW_VALUE';
$new_str = str_replace("{{search_tag}}", $val, $str);
//outputs: https://www.toto.com/search/10/NEW_VALUE.html#_his_

How to get string after a symbol

$data['subject']= "Languages > English";
//This is how I get the string before the symbol '>'.
$subject = substr($data['subject'], 0, strpos($data['subject'], "> "));
But Now I need to get word after the '>' symbol. How do I alter the code above?
Or using explode :
$array = explode(' > ', $data['subject']);
echo $array[0]; // Languages
echo $array[1]; // English
https://php.net/substr
$subject = substr($data['subject'], strpos($data['subject'], "> "));
But you should have a look at explode : https://php.net/explode
$levels = explode(" > ", $data['subject']);
$subject = $levels[0];
$language = $levels[1];
If you want the data before and after the >, I would use an explode.
$data['subject'] = "Languages > English";
$data['subject'] = array_map('trim', explode('>', $data['subject'])); // Explode data and trim all spaces
echo $data['subject'][0].'<br />'; // Result: Languages
echo $data['subject'][1]; // Result: English
You can do this way,
your string is converted in an array
then you keep the last value of your array
$data['subject']= "Languages > English";
$subject = end(explode('>',$data['subject']));

Uppercase for first letter with php

How can I convert to uppercase for the following example :
title-title-title
Result should be:
Title-Title-Title
I tried with ucwords but it converts like this: Title-title-title
I currently have this:
echo $title = ($this->session->userdata('head_title') != '' ? $this->session->userdata('head_title'):'Our Home Page');
In this particular string example, you could explode the strings first, use that function ucfirst() and apply to all exploded strings, then put them back together again:
$string = 'title-title-title';
$strings = implode('-', array_map('ucfirst', explode('-', $string)));
echo $strings;
Should be fairly straightforward on applying this:
$title = '';
if($this->session->userdata('head_title') != '') {
$raw_title = $this->session->userdata('head_title'); // title-title-title
$title = implode('-', array_map('ucfirst', explode('-', $raw_title)));
} else {
$title = 'Our Home Page';
}
echo $title;
echo str_replace(" ","-",ucwords(str_replace("-"," ","title-title-title")));
Fiddle
Output:
Title-Title-Title
Demo
Not as swift as Ghost's but a touch more readable for beginners to see what's happening.
//break words on delimiter
$arr = explode("-", $string);
//capitalize first word only
$ord = array_map('ucfirst', $arr);
//rebuild the string
echo implode("-", $ord);
The array_map() applies callback to the elements of the given array. Internally, it traverses through the elements in our word-filled array $arr and applies the function ucfirst() to each of them. Saves you couple of lines.
Edit #2
This isn't working for the new information added to op, as there is an answer this won't be updated to reflect that.
Edit #1
$var = "title-title-title";
$var = str_replace (" ", "_", ucwords (str_replace (" ", "_", $var));
Old, non-working
$var = "title-title-title";
$var = implode("-", ucwords (explode("-", $var)));
try the following:
$str='title-title-title';
$s='';
foreach(explode('-',$str) as $si){
$s.= ($s ? "-":"").ucfirst($si);
}
$s should be Title-Title-Title at this point

Convert a string to two doubles

If I have a string like this: '(1.23123, 4.123123)'
How would I convert it to two doubles?
$items = explode(',', $string);
$n1 = $items[0] // (1.23123
My attempts:
floatval($n1) // 0
(double) $n1 // 0
How can I convert?
You need to trim those parenthesis around your string . Use trim inside your explode by passing the parentheses as the charlist.
$items = explode(',', trim($str,')('));
The code
<?php
$str='(1.23123, 4.123123)';
$items = explode(',', trim($str,')('));
$items=array_map('floatval',$items);
echo $n1 = $items[0]; // "prints" 1.23123
echo $n2 = $items[1]; // "prints" 4.123123
array_map('floatval', $array) for your array
Try this code
$string = "(1.23123, 4.123123)";
preg_match_all("/[\d\.]+/", $string, $matches);
print_r($matches[0]);

PHP function that convert 'a,b' to ' "a","b" ' [duplicate]

This question already has answers here:
Add quotation marks to comma delimited string in PHP
(5 answers)
Closed 1 year ago.
I have a variable with string value of 'laptop,Bag' and I want it to look like ' "laptop","Bag" 'or "laptop","Bag". How could I do this one? Is there any php function that could get this job done? Any help please.
This would work. It first, explodes the string into an array. And then implodes it with speech marks & finishes up by adding the opening & closing speech mark.
$string = "laptop,bag";
$explode = explode(",", $string);
$implode = '"'.implode('","', $explode).'"';
echo $implode;
Output:
"laptop","bag"
That's what str_replace is for:
$result = '"'.str_replace(',', '","', $str).'"';
This would be very easy to do.
$string = 'laptop,bag';
$items = explode(',', $string);
$newString = '"'.implode('","', $items).'"';
That should turn 'laptop,bag' into "laptop","bag".
Wrapping that in a function would be as simple as this:
function changeString($string) {
$items = explode(',', $string);
$newString = '"'.implode('","', $items).'"';
return $newString;
}
I think you can explode your string as array and loop throw it creating your new string
function create_string($string)
{
$string_array = explode(",", $string);
$new_string = '';
foreach($string_array as $str)
{
$new_string .= '"'.$str.'",';
}
$new_string = substr($new_string,-1);
return $new_string;
}
Now you simply pass your string the function
$string = 'laptop,Bag';
echo create_string($string);
//output "laptop","Bag"
For your specific example, this code would do the trick:
<?php
$string = 'laptop,bag';
$new_string = ' "' . str_replace(',', '","', $string) . '" ';
// $new_string: "laptop","bag"
?>
That code would also work if you had more items in that list, as long as they are comma-separated.
Use preg_replace():
$input_lines="laptop,bag";
echo preg_replace("/(\w+)/", '"$1"', $input_lines);
Output:
'"laptop","Bag"'
I think you can perform that using explode in php converting that string in to an array.
$tags = "laptop,bag";
$tagsArray = explode(",", $tags);
echo $tagsArray[0]; // laptop
echo $tagsArray[1]; // bag
Reference
http://us2.php.net/manual/en/function.explode.php
related post take a look maybe could solve your problem.
How can I split a comma delimited string into an array in PHP?

Categories