I have string that will look like this:
$string = "hello, my, name, is, az";
Now I just wanna echo whatever is there before first comma. I have been using following:
echo strstr($this->tags, ',', true);
and It has been working great, but the problem it only works php 5.3.0 and above. I am currently on PHP 5.2.
I know this could be achieve through regular express by pregmatch but I suck at RE.
Can someone help me with this.
Regards,
<?php
$string = "hello, my, name, is, az";
echo substr($string, 0, strpos($string, ','));
You can (and should) add further checks to avoid substr if there's no , in the string.
Use explode than,
$arr = explode(',', $string,2);
echo $arr[0];
You can explode this string using comma and read first argument of array like this
$string = "hello, my, name, is, az";
$str = explode(",", $string, 2);
echo $str[0];
$parts = explode(',',$string);
echo $parts[0];
You can simple use the explode function:
$string = "hello, my, name, is, az";
$output = explode(",", $string);
echo $output[0];
Too much explosives for a small work.
$str = current(explode(',', $string));
Related
I have a PHP function which converts #Hashtag into a link...
function convertHashtags($str) {
$regex = "/#+([a-zA-Z0-9_]+)/";
$str = preg_replace($regex, '$0', $str);
return($str);
}
It work properly when I use it with a common string
$string = "Hello #World";
$string = convertHashtags($string);
(in this case an output would be: Hello #World
But when I'm trying to insert something from my database to that string it displays, but without that function's effect…
$string = $row["content"];
$string = convertHashtags($string);
(an output: Hello #World)
I am new to the PHP and MySQL stuff… Certainly, there are many things I don't know yet :D
What's wrong with this function?
Thanks!
function convertHashtags($str){
list($str1, $str2) = explode("#", $str) ;
$str2 = '#'.$str2.'';
$str = $str1." ".$str2 ;
return($str);
}
Can you use the above function and test with database entry?
Oh well, I just add new element to a database and it works!
I was testing it on old elements, they was inserted before I wrote the function.
I should try it before writing a this, my bad… thanks for help anyway!
$string = $row["content"];
$string = (string)$string;
$string = convertHashtags($string);
Use the above code and it will work.
Lets say I have this string:
CNVFJD-0905-05-BX
CNV will always be there , same with the first '-'.
What I need is to 'extract' whats between CNV and the first line(in this example I would need 'FJD'.
I don't really know how to approach this.
Thank you.
You can use substr, try this
substr($sring, 3, strpos($string, '-'));
With sscanf:
$str = "CNVFJD-0905-05-BX";
sscanf($str, "CNV%[A-Z]-", $result);
echo $result;
$parts = explode("-", $string);
$result = substr($parts[0], 3);
See here for working example https://3v4l.org/pRJ3e
My string:
'KCP-PRO;first_name last_name;address;zipcode;country' //for example: 'KCP-PRO;Jon Doe;Box 564;02201;USA'
or
'KCP-SAT-PRO;first_name last_name;address;zipcode;country'
How can i change the first part (KCP-PRO or KCP-SAT-PRO) and change it to (KCP,PRO or KCP,SAT,PRO)? The outcome has to be:
'KCP,PRO;first_name last_name;address;zipcode;country'
or
'KCP,SAT,PRO;first_name last_name;address;zipcode;country'
I haven't tried the code myself but I guess this will do the trick
$string = 'KCP-SAT-PRO;first_name last_name;address;zipcode;country';
$stringExploded = explode(';', $string);
$stringExploded[0] = str_replace('-', ',', $stringExploded[0]);
$output = implode(';', $stringExploded);
//output should be KCP,SAT,PRO;first_name last_name;address;zipcode;country
Hope this helps :)
Or you can use preg_replace_callback function with the following regex
^[^;]*
So your code looks like as
echo preg_replace_callback("/^[^;]*/",function($m){
return str_replace("-",',',$m[0]);
},"KCP-SAT-PRO;first_name last_name;address;zipcode;country");
I'm new to PHP and I have a problem.
I need delete all chars since a symbol (Sorry for my bad english , i'm from argentina)
I have this text :
3,94€
And I need the text is as follows:
3,94
I tried this by multiple ways but it didn't work.
There are a few ways you can do this:
Using strpos:
$string = '3,94€';
echo substr($string, 0, strpos($string, '&'));
or using strstr:
// Requires PHP 5.3+ due to the true (before_needle) parameter
$string = '3,94€';
echo strstr($string, '&', true);
or using explode:
// Useful if you need to keep the € part for later
$string = '3,94€';
list($part_a, $part_b) = explode('&', $string);
echo $part_a;
or using reset:
$string = '3,94€';
echo reset(explode('&', $string));
The best suited in your case would be to use strpos to find the first occurrence of & in the string, and then use substr to return the string from the begining until the value returned by strpos.
Another posibility is clean the number and then round it:
<?php
//Option 1: with regular expresions:
$val = '3,94€';
$res = preg_replace('/[^0-9.,]/','',$val);
var_dump($res);
//Option 2: with filter functions:
$res2 = filter_var($val, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND);
var_dump($res2);
//Output: 3,948364
//If you want to round it:
$res = substr($res, 0, 4);
var_dump($res);
?>
You can use regex: https://regex101.com/r/uY0kH3/1
It will work in preg_match() function.
You could use str_replace.
<?php
$string = 3,94€
$final = str_replace('€' ,'', $string);
echo $final;
I want to be able to specify an index in a string and remove it.
I have the following:
"Hello World!!"
I want to remove the 4th index (o in Hello). Here would be the end result:
"Hell World!!"
I've tried unset(), but that hasn't worked. I've Googled how to do this and that's what everyone says, but it hasn't worked for me. Maybe I wasn't using it right, idk.
This is a generic way to solve it:
$str = "Hello world";
$i = 4;
echo substr_replace($str, '', $i, 1);
Basically, replace the part of the string before from the index onwards with the part of the string that's adjacent.
See also: substr_replace()
Or, simply:
substr($str, 0, $i) . substr($str, $i + 1)
$str="Hello World";
$str1 = substr($str,0,4);
$str2 = substr($str,5,7);
echo $str1.$str2;
This php specific of working with strings also bugged me for a while. Of course natural solution is to use string functions or use arrays but this is slower than directly working with string index in my opinion. With the following snippet issue is that in memory string is only replaced with empty � and if you have comparison or something else this is not good option. Maybe in future version we will get built in function to remove string indexes directly who knows.
$string = 'abcdefg';
$string[3] = '';
var_dump($string);
echo $string;
$myVar = "Hello World!!";
$myArray = str_split($myVar);
array_splice($myArray, 4, 1);
$myVar = implode("", $myArray);
Personal I like dealing with arrays.
(Sorry about lack of code brackets putting this up via my phone)
I think can create a function and call it like this
function rem_inx ($str, $ind)
{
return substr($str,0,$ind++). substr($str,$ind);
}
//use
echo rem_inx ("Hello World!!", 4);