Use preg_replace with items from mysql_fetch_array - php

OK, so I'm not sure why but when i try to use preg_replace() on array items returned with mysql_fetch_array() I get no results but if I take those same results, make an array out of them it works fine.
Example
$pattern = '/^.*\b([0-9]{6}\s\D\d|[0-9]{6}\s\D[0-9]{2}|[0-9]{7}\s\D[0-9]{2})\b.*$/';
$replace = '$1';
while($row = mysql_fetch_assoc($result))
{
$data[] = $row['description'];
}
foreach($data as $datas)
{
echo preg_replace($pattern, $replace, $datas) . '<br>';
echo '<br>';
}
all this does is return no matches, but if I copy those results and put them in an array it works fine and I know it's not the regex because I tested that before I got this far. Here is an example of the items working when I define them in a seperate array:
$subject = array(
"Suzuki DR100 ignition pickup. Wires to the coils have been repaired and one needs to be repaired again. 082862 S15 <img src = \">http://www.roofis27.com/motorcycle/12_08_28/061.JPG\">",
"1997 Suzuki VZ800 Marauder, Kick stand and spring. Chrome on end is starting to chip. 0121103 S19 <img src = \">http://www.roofis27.com/motorcycle/13_01_21/103.JPG\">",
"Honda 1982 VF750C Magna right-side radiator trim panel. Good, damage-free condition. Needs cut and polished. Cheap, fast shipping! 011425 H6 <img src=\">http://www.roofis27.com/motorcycle/10_01_14/030.JPG\"> n=\">",
);
foreach ($subject as $subjects)
{
echo preg_replace($pattern, $replace, $subjects) . '<br>';
}
Returns:
082862 S15
0121103 S19
011425 H6
So my question is how can i use preg_replace() on an array returned from mysql_fetch_array()?
or how else can you search and replace these codes within MySQL.
Just so I'm sure to make myself clear this time:
I have a MySQL database that has these location codes within the product_description, they are all right before the <img> tag within a long product description. I need them placed into the the model column of the database, which is in another table. I have no problem using SQL to do this, but cannot get these location codes removed from the description & by themselves. MySQL does not allow regex to be used this way that i am aware of so I'm trying to do it with php instead.
Any help please! I'm really stuck.....

After talking to some people, apparently there are characters in the return from mysql_fetch_array() that are invisible and that's why the regexp does not work. I got way better results using preg_match() as follows:
foreach($data as $datas)
{
$pattern = '([0-9]{6}\s\D[0-9]{2}|[0-9]{6}\s\D\d|[0-9]{7}\s\D[0-9]{2})';
preg_match($pattern, $datas, $matches, PREG_OFFSET_CAPTURE);
foreach($matches as $match){
echo $match[0] . '<br>';
}
}

Related

PHP Replace [plugin-something] with include_once

I am fairly new to 'advanced' PHP and i am struggling with this problem for a week now. I have read almost everything on this forum to find a solution, but nothing worked quite well.
I want to get content from a database and replace all text like [plugin-example] with an include_once(example.php)
The reason behind it is to make something similar like a 'shortcode' for a content management system.
I tried several codes, and this one is the nearest to my solution (i think/hope). The problem is that this replaces both [plugins] with only the last match value (slider in this case).
<?php
//get database row
while($page = mysqli_fetch_array($hl_page)){
//regex
$search = "/\[plugin-(.*)\]/";
//get all matches with search
$content = htmlspecialchars_decode("This is my blog
[plugin-blog]
And this is my slider
[plugin-slider]");
preg_match_all($search, $content , $matches);
foreach($matches[1] as $match) {
$match;
}
enter code here
//get file
ob_start();
include_once(''.$match.'.php');
$replace = ob_get_contents();
ob_end_clean();
//echo content from page
echo preg_replace($search,$replace,$content);
}?>
That code outputs:
textual content
[the include_once('slider.php')] <---should be blog.php
more textual content
[the include_once('slider.php'] <--- this one is good
All help will be really appreciated!
Put the replacement inside the foreach loop:
foreach($matches[1] as $match) {
//get file
ob_start();
include_once(''.$match.'.php');
$replace = ob_get_contents();
ob_end_clean();
$content = preg_replace($search, $replace, $content);
}
echo $content;
If I have well understand what you are trying to do, a more easy way using preg_split:
$content = <<<'EOD'
This is my blog
[plugin-blog]
And this is my slider
[plugin-slider]
EOD;
$parts = preg_split('~\[plugin-([^]]+)]~', $content, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($parts as $k => $v) {
if ($k & 1)
include_once("$v.php");
else
echo htmlspecialchars_decode($v);
}

Extracting links from a piece of text in PHP except ignoring image links

I have this piece of text, and I want to extract links from this. Some links with have tags and some will be out there just like that, in plain format. But I also have images, and I don't want their links.
How would I extract links from this piece of text but ignoring image links. So basically and google.com should both be extract.
string(441) "<p class="fr-tag">Please visit https://www.google.co.uk/?gfe_rd=cr&ei=9P2DVaW2BMWo8wfK74HYCg and this link should be filtered and this http://d.pr/i/1i2Xu <img class="fr-fin fr-tag" alt="Image title" src="https://cft-forum.s3-us-west-2.amazonaws.com/uploads%2F1434714755338-Screen+Shot+2015-06-19+at+12.52.28.png" width="300"></p>"
I have tried the following but its incomplete:
$dom = new DOMDocument();
$dom->loadHTML($html);
$tags = $dom->getElementsByTagName('a');
foreach ($tags as $tag) {
$hrefs[] = $tag->getAttribute('href');
Using just that one string to test, the following works for me:
$str = '<p class="fr-tag">Please visit https://www.google.co.uk/?gfe_rd=cr&ei=9P2DVaW2BMWo8wfK74HYCg and this link should be filtered and this http://d.pr/i/1i2Xu <img class="fr-fin fr-tag" alt="Image title" src="https://cft-forum.s3-us-west-2.amazonaws.com/uploads%2F1434714755338-Screen+Shot+2015-06-19+at+12.52.28.png" width="300"></p>';
preg_match('~a href="(.*?)"~', $str, $strArr);
Using a href ="..." in the preg_match() statement returns an array, $strArr containing two values, the two links to google.
Array
(
[0] => a href="https://www.google.co.uk/?gfe_rd=cr&ei=9P2DVaW2BMWo8wfK74HYCg"
[1] => https://www.google.co.uk/?gfe_rd=cr&ei=9P2DVaW2BMWo8wfK74HYCg
)
I would try something like this.
Find and remove images tags:
$content = preg_replace("/<img[^>]+\>/i", "(image) ", $content);
Find and collect URLs.
preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $content, $match);
Output Urls:
print_r($match);
Good luck!
I played around with this a lot more and have an answer that may better suit what you are trying to do with a bit of "future proofing"
$str = '<p class="fr-tag">Please visit www.google.co.uk/?gfe_rd=cr&ei=9P2DVaW2BMWo8wfK74HYCg and this link should be filtered and this http://d.pr/i/1i2Xu <img class="fr-fin fr-tag" alt="Image title" src="https://cft-forum.s3-us-west-2.amazonaws.com/uploads%2F1434714755338-Screen+Shot+2015-06-19+at+12.52.28.png" width="300"></p>';
$str = str_replace(' ',' ',$str);
$strArr = explode(' ',$str);
$len = count($strArr);
for($i = 0; $i < $len; $i++){
if(stristr($strArr[$i],'http') || stristr($strArr[$i],"www")){
$matches[] = $strArr[$i];
}
}
echo "<pre>";
print_r($matches);
echo "</pre>";
I went back and analyzed your string and noticed that if you translate the to spaces you can then explode the string into an array, step through that and if any elements contain http or www then add them to the $matches array to be processed later. The output is pretty clean and easy to work with and you also get rid of most of the html markup this way.
Something to note is that this probably isn't the best way to do this. I haven't tested with any other strings but the one you offered so there's optimization that can be done.

php preg_match_all and preg_replace.

[caption id="attachment_1342" align="alignleft" width="300" caption="Cheers... "Forward" diversifying innovation to secure first place. "][/caption] A group of 35 students from...
I'm reading this data from api. I want the text just start with A group of 35 students from.... Help me to replace the caption tag with null. This is what I tried:
echo "<table>";
echo "<td>".$obj[0]['title']."</td>";
echo "<td>".$obj[0]['content']."</td>";
echo "</table>";
$html = $obj[0]['content'];
preg_match_all('/<caption>(.*?)<\/caption>/s', $html, $matches);
preg_replace('',$matches, $obj[0]['content']);
Any help.
$pattern = "/\[caption (.*?)\](.*?)\[\/caption\]/i";
$removed = preg_replace($pattern, "", $html);
echo preg_replace("#\[caption.*\[/caption\]#u", "", $str);
In the snippet mentioned in the question, regex search pattern is incorrect. there is no <caption> in the input. its <caption id....
Second using preg_replace doesn't serve any purpose here. preg_replace expects three arguments. first should be a regex pattern for search. second the string to replace with. and third is input string.
Following snippet using preg_match will work.
<?php
//The input string from API
$inputString = '<caption id="attachment_1342" align="alignleft" width="300" caption="Cheers... "Forward" diversifying innovation to secure first place. "></caption> A group of 35 students from';
//Search Regex
$pattern = '/<caption(.*?)<\/caption>(.*?)$/';
//preg_match searches inputString for a match to the regular expression given in pattern
//The matches are placed in the third argument.
preg_match($pattern, $inputString, $matches);
//First match is the whole string. second if the part before caption. third is part after caption.
echo $matches[2];
// var_dump($matches);
?>
if you still want to use preg_match_all for some reason. following snippet is modification of the one mentioned in question -
<?php
//Sample Object for test
$obj = array(
array(
'title' => 'test',
'content' => '<caption id="attachment_1342" align="alignleft" width="300" caption="Cheers... "Forward" diversifying innovation to secure first place. "></caption> A group of 35 students from'
)
);
echo "<table border='1'>";
echo "<td>".$obj[0]['title']."</td>";
echo "<td>".$obj[0]['content']."</td>";
echo "</table>";
$html = $obj[0]['content'];
//preg_match_all will put the caption tag in first match
preg_match_all('/<caption(.*?)<\/caption>/s', $html, $matches2);
//var_dump($matches2);
//use replace to remove the chunk from content
$obj[0]['content'] = str_replace($matches2[0], '', $obj[0]['content']);
//var_dump($obj);
?>
Thank you guys. I use explode function to do this.
$html = $obj[0]['content'];
$code = (explode("[/caption]", $html));
if($code[1]==''){
echo $code[1];
}

Merging preg_match_all and preg_replace

I have some code running which finds out hashtags in the string and turns them into links. I have done this using preg_match_all as shown below:
if(preg_match_all('/(#[A-z_]\w+)/', $postLong, $arrHashTags) > 0){
foreach ($arrHashTags[1] as $strHashTag) {
$long = str_replace($strHashTag, ''.$strHashTag.'', $postLong);
}
}
Also, for my search script, I need to bold the searched keywords in the result string. Something similar to the below code using preg_replace:
$string = "This is description for Search Demo";
$searchingFor = "/" . $searchQuery . "/i";
$replacePattern = "<b>$0<\/b>";
preg_replace($searchingFor, $replacePattern, $string);
The problem that I am having is that both have to work together and should be thrown as a combined result. One way I can think of is to run the resultant string from preg_match_all with the preg_replace code but what if the tags and the searched string are the same? The second block will bold my tag as well which is not desired.
update the code i'm running based on the answer given below but it still doesn't work
if(preg_match_all('/(#[A-z_]\w+)/', $postLong, $arrHashTags) > 0){
foreach ($arrHashTags[1] as $strHashTag) {
$postLong = str_replace($strHashTag, ''.$strHashTag.'', $postLong);
}
}
And immediately after this, i run this
$searchingFor = "/\b.?(?<!#)" . $keystring . "\b/i";
$replacePattern = "<b>$0<\/b>";
preg_replace($searchingFor, $replacePattern, $postLong);
Just so you know, this is all going inside a while loop, which is generating the list
You just need to modify you the search pattern to avoid ones that start with a '#'
$postLong = "This is description for Search Demo";
if(preg_match_all('/(#[A-z_]\w+)/', $postLong, $arrHashTags) > 0){
foreach ($arrHashTags[1] as $strHashTag) {
$postLong = str_replace($strHashTag, ''.$strHashTag.'', $postLong);
}
}
# This expression finds any text with 0 or 1 characters in front of it
# and then does a negative look-behind to make sure that the character isn't a #
searchingFor = "/\b.?(?<!#)" . $searchQuery . "\b/i";
$replacePattern = "<b>$0<\/b>";
preg_replace($searchingFor, $replacePattern, $postLong);
Or if you don't need an array of the available hashes for another reason, you could use preg_replace only.
$postLong = "This is description for #Search Demo";
$patterns = array('/(#[A-z_]\w+)/', "/\b.?(?<!#)" . $searchQuery . "\b/i");
$replacements = array(''.$0.'', ' "<b>$0<\/b>');
preg_replace($patterns, $replacements, $postLong);

replace link with another

I'm struggling on replacing text in each link.
$reg_ex = "/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$text = '<br /><p>this is a content with a link we are supposed to click</p><p>another - this is a content with a link we are supposed to click</p><p>another - this is a content with a link we are supposed to click</p>';
if(preg_match_all($reg_ex, $text, $urls))
{
foreach($urls[0] as $url)
{
echo $replace = str_replace($url,'http://www.sometext'.$url, $text);
}
}
From the code above, I'm getting 3x the same text, and the links are changed one by one: everytime is replaced only one link - because I use foreach, I know.
But I don't know how to replace them all at once.
Your help would be great!
You don't use regexes on html. use DOM instead. That being said, your bug is here:
$replace = str_replace(...., $text);
^^^^^^^^--- ^^^^^---
you never update $text, so you continually trash the replacement on every iteration of the loop. You probably want
$text = str_replace(...., $text);
instead, so the changes "propagate"
If you want the final variable to contain all replacements change it so something like this...
You basically are not passing the replaced string back into the "subject". I assume that is what you are expecting since it's a bit difficult to understand the question.
$reg_ex = "/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$text = '<br /><p>this is a content with a link we are supposed to click</p><p>another - this is a content with a link we are supposed to click</p><p>another - this is a content with a link we are supposed to click</p>';
if(preg_match_all($reg_ex, $text, $urls))
{
$replace = $text;
foreach($urls[0] as $url) {
$replace = str_replace($url,'http://www.sometext'.$url, $replace);
}
echo $replace;
}

Categories