$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']));
Related
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.
I am getting a string output from MYSQL DB in the following format. Quotes are included.
"Created" to "Quote Sent"
How will i save this String as 2 variables using PHP.
For Example: $var1 = 'Created'
$var2 = 'Quote Sent'
I tried with explode, but not getting the desired output.
$string = '"Created" to "Quote Sent"';
$stringParts = explode("to", $string);
$var1 = $stringParts[0];
$var2 = $stringParts[1];
Can please anyone help me on this.?
You could do somthing like this:
<?php
$str = '"Created" to "Quote Sent"';
$var1 = str_replace('"', "", explode(" to ", $str)[0]);
$var2 = str_replace('"', "", explode(" to ", $str)[1]);
?>
Also you told us you tried this, what out put DID you get?
You should only be calling explode() once. And the more appropriate call to trim the double quotes from the string is: trim() with a character mask of ".
Code: (Demo)
$str = '"Created" to "Quote Sent"';
$parts = explode(' to ', $str, 2);
$var1 = trim($parts[0], '"');
$var2 = trim($parts[1], '"');
echo $var1;
echo "\n---\n";
echo $var2;
Output:
Created
---
Quote Sent
If you are crazy for a one-liner, you can use regex.
[$var1, $var2] = preg_match('~"([^"]+)" to "([^"]+)"~', $str, $out) ? array_slice($out, 1) : ['', ''];
or
[$var1, $var2] = preg_split('~"( to ")?~', $str, 3, PREG_SPLIT_NO_EMPTY); // need to allow 3rd empty element to be found & disregarded
How would I extract the number 33 before the underscore in the following?
33_restoffilename.txt.
would something like following work?
int strPos = strpos("33_filename.txt", "_");
str num = substr ("33_filename.txt" , 0 , strPos );
there are may way to achive this:
Method 1:
$strPos = strpos("33_filename.txt", "_");
echo $num = substr("33_filename.txt" , 0 , $strPos );
Method 2:
$str = '33_filename.txt';
$str_arr = explode('_', $str);
echo $num = $str_arr[0];
If the naming convention is always number_filename.ext, you can use explode():
//Put the filename into the variable $name
$name = "33_restoffilename.txt";
//Split the name by "_"
$parts = explode("_", $name);
//Get the first part of the name from the array (position 1)
$number = $parts[0];
//Output
echo $number;
This will output
33
Use strstr().
$str = '33_filename.txt';
$num = strstr($str, '_', true);
Method - 1
$getIntegerFromBeginning = substr($string, 0, strspn($string, "0123456789"));
echo $getIntegerFromBeginning;
Method - 2
$getIntegerFromBeginning = explode("_", $string, 2);
echo $getIntegerFromBeginning[0];
Replace $string with exact string.
Use this
$string = '33_filename.txt';
$arrString = explode('_', $string);
echo $value = $arrString[0]; //Will output 33
This is really very simple we don't need these string functions in this case. Just do type casting and you will get integer number. It will be fast and easy
$pp='33_restoffilename.txt';
echo (int)$pp;
No need to make it complicated
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
I have longtext column in mysql db and want to expldoe it into array. But explode returns me only 1 string.
$temp_array_links[] = $item->links; //array value: http://google.com/ http://test.com/ http://test1.com/
$temp_string = implode(" ", $temp_array_links); //convert array to string
$info_array_links = explode(" ", $test_string); //explode string
echo 'Your link: LINK'; //should be http://google.com/ insted of http://google.com/ http://test.com/ http://test1.com/
In the third line, you used the wrong variable name $test_string, you should have used $temp_string
your $test_string should be $temp_string try
$temp_array_links = array('http://google.com/', 'http://test.com/', 'http://test1.com/');
echo $temp_string = implode(" ", $temp_array_links); //convert array to string
$info_array_links = explode(" ", $temp_string); //explode string
print_r($info_array_links);
Also for fetching array you need to use index of an array not $info_array_links[$user_id--] try $info_array_links[0] // 1, 2,3
try to implode and explode with a separated something like ;
$temp_string = implode(";", $temp_array_links); //convert array to string
$info_array_links = explode(";", $temp_string); //explode string