简体   繁体   中英

string replace text with PHP

I have

$text = '--ACT-- active --INA-- inactive';

i want to replace --ACT-- and --INA-- with value i did:

str_replace(array('--ACT--','--INA--'), array('<div class="wp"><b>1</b></div>','<div class="wp"><b>2</b></div>'), $text);

results is

<div class="wp"><b>1</b></div> Active
<div class="wp"><b>2</b></div> inactive

but I want to add active and inactive text inside div not outside div like:

<div class="wp"><b>1</b> active</div>

i don't want to add this text in str_replace, i want to get it from $text variable, any help?

Use preg_replace and capture the word after the ACT/INA and place html around it.

$text = '--ACT-- active --INA-- inactive';

// Add html to active
$text = preg_replace("/--ACT-- (\w+)/" , '<div class="wp"><b>1</b>$1</div>',$text);

// Add html to inactive and echo
Echo preg_replace("/--INA-- (\w+)/" , '<div class="wp"><b>2</b>$1</div>',$text);

https://3v4l.org/DdaFt

This assumes it's only one word after the ACT. If that is not correct then update your question with a more relevant example.

This pattern will delete text between -- and insert following text in your <div> :

$text = '--ACT-- active --INA-- inactive';

$new_text = preg_replace(
    '/((--[A-Z]{3}--) (\w+))/',
    '<div class="wp"><b>1</b> ${3}</div>',
    $text
);

echo $new_text;

Alternative (not recommended) method with one call to preg_replace assuming no duplicates exist

$text = '--ACT-- active --INA-- inactive';
$text = preg_replace('/ *--(\w+)-- */', '&$1=', trim($text)); // transform to url params
parse_str($text, $data);
var_dump($data);
/* Output
array(2) {
    ["ACT"]=>
  string(6) "active"
    ["INA"]=>
  string(8) "inactive"
}
*/
function format($data,$key,$transformation) {
    $parts = explode('%%', $transformation);
    return $parts[0].$data[$key].$parts[1];
}
echo format($data,'ACT','<div class="wp"><b>1</b>%%</div>');
//prints: <div class="wp"><b>1</b>active</div>
echo format($data,'INA','<div class="wp"><b>2</b>%%</div>');
//prints: <div class="wp"><b>2</b>inactive</div>

@JohnJack You can do it with str_replace by doing https://3v4l.org/lj6ct

$text = '--ACT-- active --INA-- inactive user --EDI-- Banned user';
$text = str_replace(array('--ACT--','--INA--','--EDI--','%%'), array('<div class="wp"><b>1</b>','%%<div class="wp"><b>2</b>','%%<div class="wp"><b>3</b>','</div>'), $text.'%%');
print $text;
//prints: <div class="wp"><b>1</b> active </div><div class="wp"><b>2</b> inactive user </div><div class="wp"><b>3</b> Banned user</div>

It is terrible idea but it works.

Simple solution :

$text = '--ACT-- active --INA-- inactive';
$textAfter= preg_replace('/--ACT-- +(\w+) +--INA-- +(\w+)/',
    '<div class="wp"><b>1</b>${1}</div> <div class="wp"><b>2</b>${2}</div>',
    $text);
echo $textAfter;
//prints: <div class="wp"><b>1</b>active</div> <div class="wp"><b>2</b>inactive</div>

A useful solution based on that would be :

function param($text, $name, $transformation) {
    return preg_replace("/.*$name +(\w+).*/",$transformation, $text);
}
$text = '--ACT-- active --INA-- inactive';

echo param($text,'--ACT--','<div class="wp"><b>1</b>${1}</div>');
//prints: <div class="wp"><b>1</b>active</div>

echo param($text,'--INA--','<div class="wp"><b>2</b>${1}</div>');
//prints: <div class="wp"><b>2</b>inactive</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM