cjackson
cjackson

Reputation: 93

php: preg_match and preg_replace

I'm unsure of how to do the following...

I need to search through a string and match all instances of a forward slash and certain letters. This is for word modifications that users have the ability to input and I want them to be able to modify individual words.

Here is an example string

Hello, isn't the weather just absolutely beautiful today!?

What I'd like the user to be able to do is something like this

Hello, isn't the /bo weather just /it beautiful today!?

take note of the /bo and /it

what I'd like to do is have a preg_match and or preg_replace statement that finds and replaces the instances of /bo and /it and converts them instead into html tags such as bolded html tag and italics html tag (i cant type them here or they get converted into actual html. but wrap themselves around the word immediately following the /bo so in this example it would wind up being

Hello, isn't the <b>weather</b> just <i>beautiful</i> today!?

Any ideas how I could do this with a regex?

Once the conversions are done i'll do the standard sanitizing before inserting the data into the database along with prepared statements.

Upvotes: 2

Views: 2429

Answers (3)

splash58
splash58

Reputation: 26153

$string = "Hello, isn't the /bo weather just /it beautiful /bo today!?";

var_dump(preg_replace (array('/\/bo\s(\w+)/', '/\/it\s(\w+)/'), array('<b>$1</b>', '<i>$1</i>'), $string));

"Hello, isn't the weather just beautiful today!?"

Upvotes: 2

Carsten Massmann
Carsten Massmann

Reputation: 28196

The regexp

/\/(bo|it)\s+([\S]+)(?=\b)/g

and replacement string

<$1>$2</$1>

would almost do it:

Hello, isn't the <bo>weather</bo> just <it>beautiful</it> today!?

But the tags are not quite right yet ... They need to be single letters. :-(

Try here: https://regex101.com/r/oB9gT0/1

2. Edit - a bit late, but now it works:

$str=preg_replace('/\/([bi])((?<=b)o|(?<=i)t)\s+([\w]+)/','<$1>$3</$1>',$str);

will now deliver the correct result:

Hello, isn't the <b>weather</b> just <i>beautiful</i> today!?

see here: https://regex101.com/r/oB9gT0/3

Upvotes: 1

Kamehameha
Kamehameha

Reputation: 5473

You can use preg_replace_callback for this purpose.
This basically calls a callback method for every match that occurs.
Inside the callback, you can perform the replacement according to your conditions
(bold for bo, italics for it, heading for he et al.)

Something like this -

$str = "Hello, isn't the /bo weather just /it beautiful today!?";
$regex = "/\/(.*?)\s+(.+?)\b/";

function callback($matches){
    $type = $matches[1];
    $text = $matches[2];
    switch($type){
        case "bo":
            return "<b>".$text."</b>";
            break;
        case "it":
            return "<i>".$text."</i>";
            break;
        default:
            return $text;
    }   
}

$resp = preg_replace_callback(
    $regex,
    "callback",
    $str
);

var_dump($resp);

/*
OUTPUT- 
Hello, isn't the <b>weather</b> just <i>beautiful</i> today!?
*/

This example could be extended further by checking for various types and invalid types

Upvotes: 1

Related Questions