How to Replace Part of a String in PHP

How do I replace part of a string in PHP?

Simply use str_replace:

$text = str_replace(' ', '_', $text);

You would do this after your previous substr and strtolower calls, like so:

$text = substr($text,0,10);
$text = strtolower($text);
$text = str_replace(' ', '_', $text);

If you want to get fancy, though, you can do it in one line:

$text = strtolower(str_replace(' ', '_', substr($text, 0, 10)));

PHP Replace part of a string before the substring I search for

You can use strpos() and substr_replace() in combination to get your desired behavior:

$str = "The antelope is hungry today"; // str to search

$needle = "hungry"; // str to search for

// returns beginning index of 'hungry' (16 in this case)
$start_index = strpos($str, $needle);

$result = substr_replace($str, "xxx", ($start_index - 3), 3);

echo $result; // prints : The antelope xxxhungry today

See this phpfiddle for my working example.

How do I replace certain parts of my string?

strtr ($str, array ('a' => '<replacement>'));

Or to answer your question more precisely:

strtr ("Hello, my name is Santa", array ('a' => '<replacement>'));

How can i replace a portion of string starting from the next 4 characters?

one-liner solution.

$string= substr_replace("8487013103", "****", 4, 4);
echo $string;

Is there a way I can replace part of a string in twig?

You can extend twig with any PHP logic you want, e.g.

use Slim\Views\Twig;
use Slim\Views\TwigExtension;

$container['view'] = function ($container){
$twig = new Twig(__DIR__.'/../resources/views');

$twig->addFilter(new Twig_SimpleFilter('obfuscate', function($value, $char = '*', $visible = 4) {
if ($visible % 2 != 0) $visible++;
if (strlen($value) <= $visible) return str_repeat($char, strlen($value));
return substr($value, 0, floor($visible) / 2).str_repeat($char, strlen($value) - $visible).substr($value, -1 * (floor($visible) / 2));
}),

return $twig;
};

{{ 'john.doe@gmail.com' | obfuscate }} {# output: jo**************om  #}
{{ 'john.doe@gmail.com' | obfuscate('-', 10) }} {# output: john.--------l.com #}
{{ '123456' | obfuscate }} {# output: 12**56 #}
{{ '123' | obfuscate }} {# output: *** #}
{{ '1' | obfuscate }} {# output: * #}
{{ '+32497123456' | obfuscate }} {# output: +3********56 #}

How can I hide/replace a part of a string in PHP?

if you alltime wants to show 1st and last 3 letters and in between some * than you can do it by sub-string and string concat method

$str = "hello this is my password"; 
echo substr($str,0,3)."******".substr($str,(strlen($str))-3,3);

php/regex: How to replace part of a found pattern, but leaving the rest as it is?

If your regex matches only the relevant part, it should be no problem that it replaces the complete match (like preg_replace('/X/', 'Z', $string)).

But if you need the regex to contain parts that should not be replaced, you need to capture them and insert them back:

preg_replace('/(non-replace)X(restofregex)/', '$1Z$2', $string);

Replace part of a string from a specific character

Use strpos to find the position of the '?' in your string. Then pass that in as the 3rd (length) parameter of substr to get all the characters in the string from 0 through (length).

<?php
$str = 'profile_photo.jpg?1491781969';
echo substr($str, 0, strpos($str, '?'));
?>


Related Topics



Leave a reply



Submit