2

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.

1
  • You can type them here. Just use the code block. If you are not able to, then type it...someone here will do it for you Commented Jun 25, 2015 at 6:46

3 Answers 3

2
$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!?"

Sign up to request clarification or add additional context in comments.

2 Comments

@Uchiha You always correct me if something goes wrong :)
@Uchiha, Just do not tell anyone :) My mom trains the brain solving Sudoku I decided to use this website for this purpose and, before retirement, recall programming, that i did not use last 25 years)
1

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.