use str_replace to remove after # [duplicate] - php

This question already has answers here:
removing #email.com from string in php
(6 answers)
Closed 4 years ago.
How can I use the str_replace() without using an array of words for turning:
some#mail.com
into
some
So, everything after the '#' sign includes # as well.
How can I do that?
As an example, i will type 'adminofsite#xxxwwweeerandomstuff.com'
and the output will be: 'adminofsite'.

use strstr
$email="john#doe.com"
$user = strstr($email, '#', true);
echo $user;

$str = "some#mail.com"
$str = substr($str,0,strpos($str,"#"))

function stripEmailDomain($string){
return substr($string, 0, strpos($string, '#'));
}

I exploded and then rebuilt the string. This will work if you are positive the string will always be an email address. Its a work around but seems flexible if you want to modify it for a specific purpose.
<?php
$explo0= explode('#',"some#mail.com");
for($i=0;$i<count($explo0);$i++){
$exploresult0.=$explo0[$i+1];
}
echo "#".$exploresult0;
?>

Related

Laravel: Regex to strip part of a url [duplicate]

This question already has answers here:
extract part of a path with filename with php
(2 answers)
Closed 2 years ago.
$string = "https:\/\/s3.us-west-2.amazonaws.com\/dev-lopes\/videos\/585903773a9_654641765275736"
My intention is to have the videos path (stripping the escaped forward slash) and the identifier set to a variable, like videos/585903773a9_654641765275736
Is this possible using str_replace or is this strictly regex required?
Try this Way. I think it will help you. I hope you will get your desire output.
<?php
// Your code here!
$string = "https:\/\/s3.us-west-2.amazonaws.com\/dev-lopes\/videos\/585903773a9_654641765275736";
//remove back slashes
$str1 = stripslashes($string);
//count total string length.
$len = strlen($str1);
//pick the position of /videos
$pos = strpos($str1,'/vid');
//split it from the url.
$break_string = substr($str1, $pos, $len);
//output: /videos/585903773a9_654641765275736
echo($break_string);
?>

How to add - in-between strings [duplicate]

This question already has answers here:
Insert string at specified position
(11 answers)
Closed 3 years ago.
How do I add - in between strings.
Let’s say for instance, I want to add - in 123456789101 at the fourth position three times thereby making it look like this : 1234-5678-9101.
Substr_replace() or str_replace has not solved the problem.
I would use combination of str_split and implode.
$code = 123456789101;
$formatted = implode('-', str_split($code, 4) );
echo $formatted; //1234-5678-9101
There are a number of ways to do this.
Use substr
$output = sprintf('%s-%s-%s', substr($string, 0,4), substr($string,4,4), substr(8,4));
Use preg_replace
$output = preg_replace('/(.{4,4})(.{4,4})(.{4,4})/', '$1-$2-$3', $string);

ucfirst() not working properly with scandinavic characters [duplicate]

This question already has answers here:
strtolower() for unicode/multibyte strings
(8 answers)
Closed 1 year ago.
How to get ucfirst() working with scandinavic characters?
$str = "SÄKYLÄ";
echo ucfirst(strtolower($str)); //prints SÄkylÄ
One possibility is use mb_convert_case() but I'd like to know if this is possible using ucfirst()
$str = "SÄKYLÄ";
echo mb_convert_case($str, MB_CASE_TITLE, "UTF-8"); //prints Säkylä
Which function is faster for string capitalization?
Your problem here is not ucfirst() it's strtolower(). You have to use mb_strtolower(), to get your string in lower case, e.g.
echo ucfirst(mb_strtolower($str));
//^^^^^^^^^^^^^^ See here
Also you can find a multibyte version of ucfirst() in the comments from the manual:
Simple multi-bytes ucfirst():
<?php
function my_mb_ucfirst($str) {
$fc = mb_strtoupper(mb_substr($str, 0, 1));
return $fc.mb_substr($str, 1);
}
Code from plemieux from the manual comment

PHP ltrim() not working as expected [duplicate]

This question already has answers here:
PHP ltrim behavior with character list
(2 answers)
Closed 8 years ago.
I have this code..
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = ltrim($homepage1, 'datastring=/mac_project');
echo $trimmed;
I get the output as folio/kitchen/images/03.jpg. It's missing the /port from the /portfolio directory.
Full output should've been /portfolio/kitchen/images/03.jpg
Why not do the simple str_replace() ?
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = str_replace('datastring=/mac_project','',$homepage1);
echo $trimmed;// "prints" /portfolio/kitchen/images/03.jpg
The second parameter for ltrim is for character_mask, which means all the chars in the list will be trimmed.
You could use str_replace(), or if you want to replace only at the beginning of the string by preg_replace():
$trimmed = preg_replace('~^datastring=/mac_project~', '', $homepage1);

Convert string in php [duplicate]

This question already has an answer here:
How to decode something beginning with "\u" with PHP
(1 answer)
Closed 8 years ago.
I have one string:
"Hello\u00c2\u00a0World"
I would like convert in:
"Hello World"
I try :
str_replace("\u00c2\u00a0"," ","Hello\u00c2\u00a0World");
or
str_replace("\\u00c2\\u00a0"," ","Hello\u00c2\u00a0World");
but not work!
Resolve!
str_replace(chr(194).chr(160)," ","Hello\u00c2\u00a0World");
If you would like to remove \u.... like patterns then you can use this for example:
$string = preg_replace('/(\\\u....)+/',' ',$input);
You are most of the way there.
$stuff = "Hello\u00c2\u00a0World";
$newstuff = str_replace("\u00c2\u00a0"," ",$stuff);
you need to put the return from str_replace into a variable in order to do something with it later.
This should work
$str="Hello\u00c2\u00a0World";
echo str_replace("\u00c2\u00a0"," ",$str);
You may try this, taken from this answer
function replaceUnicode($str) {
return preg_replace_callback("/\\\\u00([0-9a-f]{2})/", function($m){ return chr(hexdec($m[1])); }, $str);
}
echo replaceUnicode("Hello\u00c2\u00a0World");

Categories