Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex str_replace

Would it be possible to incorporate a str_replace method with a regular expression, designed to catch url strings in a simple html < textfield > input type?

I'm considering something simple like a tip for the user to set it up as follows:

This is some text and click this link here.

Obviously the word "here" is a href to the url before it (or after it, if that makes a difference). The text input is drawn from a MySQL db.

I believe the start of my solution is something along the lines of:

$regex = ''; $pg = $row['pg']; $pg = str_replace('{regex goes here}', $pg); 

But I know things are missing. And then I would just output the $pg paragraph.

like image 642
Mikkel Winther Avatar asked Nov 23 '12 11:11

Mikkel Winther


People also ask

What is the difference between Str_replace and Str_replace_all?

str_replace() replaces the first matched pattern and returns a character vector. str_replace_all() replaces all matches.

What is the difference between Str_replace and Preg_replace?

str_replace replaces a specific occurrence of a string, for instance "foo" will only match and replace that: "foo". preg_replace will do regular expression matching, for instance "/f. {2}/" will match and replace "foo", but also "fey", "fir", "fox", "f12", etc.

What is Str_replace function in PHP?

The str_replace() function replaces some characters with some other characters in a string. This function works by the following rules: If the string to be searched is an array, it returns an array. If the string to be searched is an array, find and replace is performed with every array element.

What is the regex for string?

A regular expression (regex) defines a search pattern for strings. The search pattern can be anything from a simple character, a fixed string or a complex expression containing special characters describing the pattern.


1 Answers

Check out preg_replace here, this is what you are lookin for.

// From the documentation. preg_replace($regularExpression, $replacement, $subject);  
like image 105
Wesley van Opdorp Avatar answered Sep 21 '22 13:09

Wesley van Opdorp