ERROR: preg_replace(): Delimiter must not be alphanumeric or backslash - php

I'am having trouble with a preg_replace() and I have tried to solve it for some time now.
I'am getting this error:
preg_replace: Delimiter must not be alphanumeric or backslash
Here is my code:
$id = 23;
$titleForUrl = "I_like_cookies";
$uploadShortDesc = "This is the best desc and I love it 1242";
$uploadShortDesc = preg_replace('\1242\',
'<a href="http:\/\/google.com\/freecookies\/'.$id.'-'.$titleForUrl.'">http:\/\/google.com\/freecookies\/'.$id.'-'.$titleForUrl.'.<\/a> ',
$uploadShortDesc);
echo $uploadShortDesc;
Is should echo:
This is the best desc and I love it http://google.com/freecookies/23-I_like_cookies.
I would love if someone could help me, I have put a \ in front of all / but I don't know why it won't work.

Thanks Fred -ii- for the help!
it should look like:
$uploadShortDesc = preg_replace('\1242\', 'http://google.com/freecookies/'.$id.'-'.$titleForUrl.'. ', $uploadShortDesc);
Thanks! I really should read that manual more...

Related

Regex to select url except when = is directly infront of it

I'm trying to use a regex to find and replace all URLs in a forum system. This works but it also selects anything that is within bbcode. This shouldn't be happening.
My code is as follows:
<?php
function make_links_clickable($text){
return preg_replace('!(([^=](f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9#:%_+.~#?&;//=]+)!i', '$1', $text);
}
//$text = "https://www.mcgamerzone.com<br>http://www.mcgamerzone.com/help/support<br>Just text<br>http://www.google.com/<br><b>More text</b>";
$text = "#Theareak We know this and [b][url=https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks]here[/url] [/b]is an explanation, we are trying to fix this asap! https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks aaa";
echo "<b>Unparsed text:</b><br>";
echo $text;
echo "<br><br>";
echo "<b>Parsed text:</b><br>";
echo make_links_clickable($text);
?>
All urls that occur in bb-code are following up on a = character, meaning that I don't want anything that starts with = to be selected.
I basically have that working but this results in selecting 1 extra character in in front of the string that should be selected.
I'm not very familiar with regex. The final output of my code is this:
<b>Unparsed text:</b><br>
#Theareak We know this and [b][url=https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks]here[/url] [/b]is an explanation, we are trying to fix this asap! https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks aaa<br>
<br>
<b>Parsed text:</b><br>
#Theareak We know this and [b][url=https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks]here[/url] [/b]is an explanation, we are trying to fix this asap! https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks aaa
You can match and skip [url=...] like this:
\[url=[^\]]*](*SKIP)(?!)|(((f|ht)tps?://)[-a-zA-Zа-яёЁА-Я()0-9#:%_+.\~#?&;/=]+)
See regex demo
That way, you will only match the URLs outside the [url=...] tag.
IDEONE demo:
function make_links_clickable($text){
return preg_replace('~\[url=[^\]]*](*SKIP)(?!)|(((f|ht)tps?://)[-a-zA-Zа-яёЁА-Я()0-9#:%_+.\~#?&;/=]+)~iu', '$1', $text);
}
$text = "#Theareak We know this and [b][url=https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks]here[/url] [/b]is an explanation, we are trying to fix this asap! https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks aaa";
echo "<b>Parsed text:</b><br>";
echo make_links_clickable($text);
You can use a negative lookbehind (?<!=) instead of your negated class. It asserts that what is going to be matched isn't preceded by something.
Example

How to ignore single quotes in regex using preg_replace function in PHP?

I am basically trying to transform any hash-tagged word in a string into a link:
Here is what my code looks like:
public function linkify($text)
{
// ... generating $url
$text = preg_replace("/\B#(\w+)/", "<a href=" . $url . "/$1>#$1</a>", $text);
return $text;
}
It works pretty good excepting the case when that $text contains a single quote. Here are
Example1:
"What is your #name ?"
Result: "What is your #name?" Works fine.
Example2:
"What's your #name ?"
Result: "What's your #name?" Does not work, I want
this result: "What's your #name?"
Any idea about how I can get rid of that single quote problem using PHP ?
EDIT1:
Just for info, before or after html_entity_decode($text) I got
"What's your #name?"
Something like this.
$string = "' \'' '";
$string = preg_replace("#[\\\\']#", "\'", $string);
Something is protecting your html entities. This can save your life if the string is coming from a get/post request - but iI it's from a trusted source just use html_entity_decode to convert it back. This 39-thing is a way to express the single quote as you might have realized.
if the problem is html_entities, then maybe you only need to html_entity_decode your $text
$text = preg_replace("/\B#(\w+)/", "<a href=" . $url . "/html_entity_decode($1)>#$1</a>", $text);
Thanks all for your suggestions, I've finally sorted this out with this :
html_entity_decode($str, ENT_QUOTES);

Cleaning data and removing brackets

Okay I have been having a little issue with the code below right now it pulls a bunch of data about a few stocks for me but I cant seem to get ride of the [ or ]. I thought it would be easy to remove the brackets by adding them to the end and the beginning of the preg_split but right when I do it just pops a error message.
Is there any command to remove all [ or all ] cause I know I have found a command in the past to remove all non numeric characters thank you so much btw
<?php
$Bloomberg = 'http://www.bloomberg.com/quote/AAPL:US';
$Bloomberghtml = file_get_contents($Bloomberg);
$BloombergStart = preg_split('/data_values":/',$Bloomberghtml);
$BloombergRawData = preg_split('/>/',$BloombergStart[1]);
$OpenTimeStart = preg_split('/exch_open_time":/',$BloombergRawData[0]);
$OpenTime = preg_split('/,"/',$OpenTimeStart[1]);
$CloseTimeStart = preg_split('/exch_close_time":/',$BloombergRawData[0]);
$CloseTime = preg_split('/,"/',$CloseTimeStart[1]);
$StartPriceData = preg_split('/,/',$BloombergRawData[0]);
echo $BloombergRawData[0];
echo "<br><br><br>";
echo $OpenTime[0];
echo "<br>";
echo $CloseTime[0];
echo "<br>";
print_r($StartPriceData);
?>
I'm not sure I fully understood your problem, but if you need to remove the '[' and ']' chars you can do that rather simply:
str_replace(array( '[', ']' ), '', $BloombergRawData);
Not the most elegant solution, but it should to the trick.

How can i allow only arabic characters in input text field?

I already searched here and found similar posts related to this post but i did not find a solution yet .
i tried this :
$text = "الحمد لله رب العالمين hello";
echo $is_arabic = preg_match('/\p{Arabic}/u', $text);
I add the unicode flag but if i add any English characters it is returning true ! any fix for this ?
Any idea folks ?
Thanks in advance
Use unicode flag:
$text = "الحمد لله رب العالمين";
echo $is_arabic = preg_match('/\p{Arabic}/u', $text);
here __^
If you want to match only arabic you should do:
echo $is_arabic = preg_match('/^[\s\p{Arabic}]+$/u', $text);
Update: I see I am apparently wrong about classes not being supported (though the docs at say "Extended properties such as "Greek" or "InMusicalSymbols" are not supported by PCRE" but the comment at http://php.net/manual/en/regexp.reference.unicode.php#102756 says they are supported), so I guess M42's is the better answer. They can, however, be done with ranges as follows:
$text = "الحمد لله رب العالمين";
echo $is_arabic =
preg_match('/^[\s\x{0600}-\x{06FF}\x{0750}-\x{077F}\x{08A0}-\x{08FF}\x{FB50}-\x{FDFF}\x{FE70}-\x{FEFF}\x{10E60}\x{10E60}—\x{10E7F}\x{1EE00}—\x{1EEFF}]+$/u', $text);

Replace " ’ " with " ' " in PHP

I'm grabbing a string from the database that could be something like String’s Title however I need to replace the ’ with a ' so that I can pass the string to an external API. I've used just about every variation of escaped strings in str_replace() that I can think of to no avail.
$stdin = mb_str_replace('’', '\'', $stdin);
Implementation of mb_str_replace() here: http://www.php.net/manual/en/ref.mbstring.php#107631
I mean this:
<?php
function mb_str_replace($needle, $replacement, $haystack) {
return implode($replacement, mb_split($needle, $haystack));
}
echo mb_str_replace('’', "'", "String’s Title");
It may solve encoding problems.
I have just tested this:
echo str_replace('’', "'", $cardnametitle);
//Outputs: String's Title
Edit: I believe that entries in your database have been htmlentitiesed.
Note: I'm pretty sure this is not a good solution, even though it did solve your problem I think there should be a better way to do it.
Try this
$s = "String’s Title";
$h = str_replace("’","'",$s);
echo $h;
Also can Try with preg_replace
echo preg_replace('/\’/',"'","String’s Title");
I don't know why str_replace() is not working for you.
I feel you haven't tried it in correct way.
Refer LIVE DEMO
<?php
$str = "String’s Title";
echo str_replace('’', '\'', $str) . "\n";
echo str_replace("’", "'", $str);
?>
OUTPUT:
String's Title
String's Title
UPDATE 1:
You may need to try setting the header as
header('Content-Type: text/html; charset=utf-8');
I came across a similar issue trying to replace apostrophes with underscores... I ended up having to write this (and this was for a WordPress site):
$replace = array(",","'","’"," ","’","–");
$downloadTitle = str_replace( $replace,"_",get_the_title($gallery_id));
I'm new to PHP myself, and realize this is pretty hideous code, but it worked for me. I realized it was the "’" that REALLY needed to be factored in for some reason.

Categories