Need to replace deprecated ereg_replace [duplicate] - php

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 9 years ago.
I am working for a non-profit and i'm not an expert in PHP.
I need to replace the following code:
$status = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "\\0", $status);
When I attempt to modify it to preg_replace, I get an error every different way I try to exit the code.

This will do the job:
$statut = preg_replace('~[a-z]+://[^<>\s]+[\w/]~i', '$0', $statut);
But if the goal of this replacement is to keep all urls and transform them into links, you must change the pattern a little. And, why not, test them with filter_validate_url

Related

How can this ereg code possibly be updated to preg_match? [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 2 years ago.
I have reviewed the answers to this out the wazoo. I've googled this the same.
I have a variable that works really great with php 5.4 ereg but fails miserably with the latest stuff.
ereg($user.$pass, preg_replace('/\s/', '', trim($_POST['user']).trim($_POST['pass'])))
I have tried the following with no joy:
preg_match('/^$user.$pass/',
preg_match('/{$user.$pass}/',
preg_match({$user.$pass},
preg_match('{$user.$pass}',
I don't know what to do. I've also tried rewriting the code to use different input lines, but to no avail.
Does the rest of the line need escaping as well?
Is it possible that this function just doesn't port easily into the latest php versions?
This is the complete line of code. It looks to see if a user and password match the user and password for this page and trims out spaces. (I probably should sanitize it outside of this if statement.) It also checks the SESSIONS status. It may be a bit ugly but it works great in 5.4:
elseif(((!empty($_POST['user']) and !empty($_POST['pass']) and ereg($user.$pass, preg_replace('/\s/', '', trim($_POST['user']).trim($_POST['pass'])))) or (isset($_SESSION['logged']) and ($_SESSION['logged'] === true) and isset($_SESSION['user']) and ($_SESSION['user'] == $user))) and ($_SESSION['attempts'] < 5))
ADDITIONAL: I found a way around the ereg or the preg_match. It works great.
The (main) usage difference between ereg() and preg_match is the delimiters.
So if your code was:
ereg($user.$pass, preg_replace('/\s/', '', trim($_POST['user']).trim($_POST['pass'])));
the new equivalent code should be:
preg_match('/'.$user.$pass.'/', preg_replace('/\s/', '', trim($_POST['user']).trim($_POST['pass'])));
Please note the lack of {, } or ^ in the first parameter of preg_match().

find/replace in PHP string [duplicate]

This question already has answers here:
PHP strtr vs str_replace benchmarking
(3 answers)
Replace text in a string using PHP
(3 answers)
Closed 5 years ago.
I have a PHP string in which I would like to find and replace using the strtr function, problem is I have variable fields so I won't be able to replace by name. The string contains tags like the following:
[field_1=Company]
[field_4=Name]
What makes it difficult is the "Company" and "Name" part of the "tag", these can be variable. So I basically looking for a way to replace this part [field_1] where "=Company" and "=Name" must be discarded. Can this be done?
To explain: I'm using "=Company" so users don't just see "field_1" but know the value it represents. However users are able to change the value to what they see fit.
You are probably looking for regular expressions. There is a function in PHP to do a regex replace:
http://php.net/manual/en/function.preg-replace.php
Been a while since I've worked in PHP but you might want to try something like this:
preg_replace('/field_\d/','REPLACEMENT','[field_1=Company]');
Should result in
[REPLACEMENT=Company]
If you want to replace everything except the brackets:
preg_replace('/field_\d+=\w+/','REPLACEMENT','[field_1=Company]');

How to find a character using index in PHP string? [duplicate]

This question already has answers here:
Finding a character at a specific position of a string
(5 answers)
Closed 5 years ago.
I have a string in PHP code like this:
$string = "This is a PHP code";
I want to find a character at index 3 in above string. And output must be:
s
Is there any idea to achieve this goal ?
If I understood your question properly, you want character at 3rd index then write following line ;)
echo $string[3];

remove plaintext links from post in php [duplicate]

This question already has answers here:
Regular expression for checking website url
(6 answers)
Match URL pattern in PHP using a regular expression
(8 answers)
Closed 6 years ago.
The question is fairly straight forward. I want to remove text links from posts.
$post = $_POST['text'];
//something to remove any instance of a text link
Please understand I am not trying to remove
text
Rather I just want any plaintext links removed
This is an example. All this is okay but I want the following stripped:
http://someurl.com/... or http://www.someurl.com/...
Here is what I have been trying:
$post = preg_replace(#^(http\:\/\/|https\:\/\/)?([a-z0-9][a-z0-9\-]*\.)+[a-z0-9][a-z0-9\-]*$#i, '', $posted);
I am getting error: unexpected '^', if I remove the ^ then I get an unexpected ':' error.

ereg() to preg_match() how? [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 3 years ago.
I have kind of simple regexes that i am using.
It's so simple as:
"user/"
"user/[A-z0-9_-]"
These all work fine with ereg, but not preg.
How to convert it?
Thanks
It's most probably because your're missing the delimiters. Try this:
"~user/~"
"~user/[A-z0-9_-]~"
ereg('jpg$', $query)
Turns into:
preg_match('#jpg$#', $query)

Categories