remove plaintext links from post in php [duplicate] - php

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.

Related

How to prevent PHP from renaming '&times' to 'x' symbol? [duplicate]

This question already has answers here:
Why is "&reg" being rendered as "®" without the bounding semicolon
(8 answers)
Should an ampersand be URL encoded in a query string?
(2 answers)
Closed 2 years ago.
I have with an unwanted replaced, and not able to figure out how to fix it.
When you echo the following string in PHP
echo('?hash=123&rid=111&timestamp=123');
The output is:
?hash=123&rid=111×tamp=123
Note that &timestamp has been replaced with ×tamp
I tried to escape it by using \&timestamp but that doens't work.
How can I prevent PHP replacing this?
You can reproduce this error online using http://phptester.net/
You have to escape that string, because & is a special symbol in HTML.
echo htmlspecialchars('?hash=123&rid=111&timestamp=123');
More information on the PHP site: https://www.php.net/manual/en/function.htmlspecialchars.php

How can I remove a portion of a string from the right side of a string starting at a special character [duplicate]

This question already has answers here:
Break up/parse a URL into its constituent parts in php
(1 answer)
How to remove the querystring and get only the URL?
(16 answers)
Closed 3 years ago.
I have a series of strings created as URLs as the page gets refreshed.I want to remove a portion from the right side of these strings. The strings look like:
http://funfun.ca/?image=1
http://funfun.ca/?image=14
http://funfun.ca/?image=217
and so on. The goal is for anything starting with "?" to be removed from the right side of the strings and leave the following for all of them:
http://funfun.ca/
I appreciate any help I can have to solve this
You may explode the url string for ? and save only the first part obtained:
$firstPart = explode('?', $url)[0];
Example:
echo $firstPart = explode('?', 'http://funfun.ca/?image=1')[0];
returns:
http://funfun.ca/

Character strange html [duplicate]

This question already has answers here:
How do I remove ASCII number 13?
(1 answer)
PHP remove line break or CR LF with no success
(9 answers)
How to remove ANSI-Code ("
") from string in PHP
(4 answers)
Closed 4 years ago.
Ok, maybe is simple question but i am generating a xml from custom posts of wordpress, the problem is a textarea field of acf, i put a break line here and the code generate give me this character 
 but i not find nothing about that in google and str_replace not make nothing about that. i already remove the <br /> and <br> but that character I can't.
this is my actual code about that
str_replace('
', '', str_replace('<br />', '', strip_tags($addressValue['c_restaurant_map_address'], '<br /><br/><br>')));

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];

Need to replace deprecated ereg_replace [duplicate]

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

Categories