PHP - Preg_replace Problem.
Hello.
Im working on a function: Code: [Select] function image_vote_img_replace($content) { preg_match_all('/<img[^>]+>/i',$content, $result, PREG_PATTERN_ORDER); foreach ($result[0] as $img_oryg) { $result = '<div class="test">'.$img_oryg.'</div>'; $content = preg_replace( $img_oryg, $result , $content ); } return $content; } It gets as input content from CMS. It supposed to add overlaping div to each <img src="xxxx" /> so it looks like: <div class="test"><img src="xxxx" /></div>. But instead i get: <<div class="test"><img src="xxxx" /></div>> with additional < and > at beginning and end. I thin ,that it might be first regexp problem, but in second hand - entire img is extracted properly... Thanks in advice. Martin. Similar TutorialsQuote Hi Guys, I have a small issue with doing a preg_replace on a string. Here is the string I wish to use preg_replace on Code: [Select] <?php $mystring = "This is my test string that has {['site_info']} and some more stuff {['taste']} then nothing" ?> Quote I want to do a preg_replace and replace {['site_info']} with $stuff['major']['site_info'] through the string Quote I want to replace the placeholder with real variables from the array Any help would be great I currently have some custom tags, similar to BBCode, to be used for comments on my site. I'm trying to create a [nobr] tag (note: I know the no break tag will work in many major browsers, but it's deprecated, so I'm trying to be compliant). The idea is that if I encounter the [nobr] tag, I need to remove all line breaks. Here's the best I can do, but it's not working as I had hoped. Here's what I currently use to replace line breaks with a break tag. Code: [Select] $str=str_replace("\r\n","<br>",$str); Here's my attempt at introducing this new tag. Code: [Select] function BbToHtml($str) { $before=preg_replace("/(.+)\[nobr\](.+)\[\/nobr\](.+)/Usi","\\1",$str); $during=preg_replace("/(.+)\[nobr\](.+)\[\/nobr\](.+)/Usi","\\2",$str); $after=preg_replace("/(.+)\[nobr\](.+)\[\/nobr\](.+)/Usi","\\3",$str); $during=str_replace("\r\n","",$during); if($during != "") { // If the [nobr] tag was found, go with the new string. $str = $before.$during.$after; } else { // If the [nobr] tag was not found, go ahead like normal. $str=str_replace("\r\n","<br>",$str); } return $str; I can't get that working. Any ideas on how to fix that, or a completely alternate method of doing it? Thank you! Hi, I'm kind of new to PHP and very new to Regular Expressions and I was wondering if someone could help run through this code snippet from a callback function in a Joomla plugin called Affliates Feed. So far I have this is passed a pointer to variable item (I understand this is effectively the same as passing the variable but a lot less CPU intensive. Creates an array with keys and values Now the for loop I am stuck with, I understand the interation through the array. But what is preg_replace returning and why pass it back that way hasn't it already done its job? Can any one help? And please feel free to be as basic as possible :help: Thanks, Adam Code: [Select] function gardening(&$item) { #renaming categories, not the most efficient use for example strtolower first, and use only lowercase in the rename array $rename=array( ' Gardening?' => '', 'Climber Plants' => 'Climbing Plants', 'Seeds.*Bulbs' => 'Seeds Bulbs', 'Plant protection' => 'Horticultural goods', 'Garden structures' => 'Garden buildings',); foreach ( $rename as $k => $v ) { list($item['menu_1'],$item['menu_2'])=preg_replace("#$k#",$v,array($item['menu_1'],$item['menu_2'])); } $item['menu_1']=ucfirst(strtolower($item['menu_1'])); $item['menu_2']=ucfirst(strtolower($item['menu_2'])); $item['menu_3']=ucfirst(strtolower($item['menu_3'])); $item['menu_4']=ucfirst(strtolower($item['menu_4'])); } Hi there! I need to echo some text from a database table, this text should be linked to a url stored in the same database. I am trying to do this with preg_replace but I am terrible at it! I keep getting this error: Warning: Wrong parameter count for preg_replace() while($row = mysql_fetch_array( $result )) { $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; echo preg_replace ($reg_exUrl, $row['title'); Any suggestion? Thanks After days of searching I found this thread http://www.phpfreaks.com/forums/index.php?topic=236954.0 that helped me do what I was trying to do. I've gotten the code to create and rename my html document but I can't figure out how to use the preg_replace part. This is my template with the 3 variables I want to replace with values from a form. Code: [Select] <html> <head> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <?php include_once('content.php') ?> </body> <SCRIPT language="JavaScript" SRC="javascript.js"></SCRIPT> <script type="text/javascript"> function nametitle(){ parent.iframe1.document.getElementById("div1").innerHTML = "VARIABLE1 <br>VARIABLE2"; parent.iframe1.document.getElementById("quotes").innerHTML = "VARIABLE3"; } </script> </html> I've spent hours reading and trying to understand how to make it work but it's a little over my head. Could someone help me with this? Thanks, im trying to do a complex(i think) prg replace but i keep getting the error: Code: [Select] Failed evaluating code: $lang->showing-1,1 the code i am trying to replace is: Code: [Select] <asf: lang[showing-<asf: var[num_report_alerts]>,<asf: var[num_report_alerts]>]> and the preg_replace is: Code: [Select] $content = preg_replace('|\<asf: lang\[(.*?)-(.*?)\]\>|e', '$lang->replace_vars($lang->//1, array(\\2))', $content); its supposed to take everything inside <asf: lang[]> and echo \\1 which would be "showing" and then add \\2 into an array which would be "array(1,1)". So the output would be showing 1 of 1. Anyone know where im going wrong? i have: $msg = ereg_replace("[^A-z0-9]"," ",$msg); $msg = ereg_replace(" "," ",$msg); $msg = ereg_replace(" "," ",$msg); how will i apply preg_replace please??? thank you I am trying to pull out the image HTML tag but its not working as expected. Here is what I am doing $removeimagetag='/<a href/'; $replacwith = "illegal"; $_POST['descr']=preg_replace($removeimagetag,$replacwith,$_POST['descr']); in the 'descr' there is a <a href="http://s635.photobucket.com...... When it post it does not strip out the <a href So the image is displayed. I tried a modified version to test to see if it work that looked like this. $removeimagetag='/goofy/'; $replacwith = "illegal"; $_POST['descr']=preg_replace($removeimagetag,$replacwith,$_POST['descr']); When the word goofy appears in the text then its replaced with the word illegal, Also I would like to make it so that it replaces the entire line from the <a href....... to the </a> with a phrase that says "this feature is not allowed" rather than just the part in the front...the <a href. Any help you can be will be greatly appreciated. I'm trying to search a string for all occurences of a pattern, then append text after that pattern. The example is:
SUM("foo") as "bar"
and have it display as:
SUM("foo") over() as "bar"
The "SUM" could be any SQL aggregate and be case insensitive. So For bonus points, let's say the valid aggregates are array('SUM', 'MAX', 'MIN', 'AVG'). My plan right now is to figure it out for SUM, then loop it replacing the aggregate.
This is what I have so far... and it is not working.
$l= 'SUM("foo") as "bar"';
$l=preg_replace('/([^"]+")/','\0 over()',$l);
Thanks,
Ryan
Hello ppl, I have this website link http://www.somesite.com/anotherpage/yetotherpage.html what i want to make is using preg_replace that the final result will be www.somesite.com Hi everyone, how do i add and ampersand symbol into preg_replace preg_replace("/[^a-zA-Z0-9\s\-\'\,\.\_]/" where do i place the symbol? & how is it possible to allow someone to do paragraphs when using preg_replace? would someone show a few good examples of hows its done? i want to remove any links which get pulled out in my general information query. Code: [Select] $general_info = $row['general_info']; $general= preg_replace('/<a href="([^<]*)">([^<]*)<\/a>/', '', $general_info); echo '<h2>General Information : <img src="images/mag.png"></h2>'; echo '<div id="featurecontent">'; echo '<p>'. ucfirst($general).'</p>'; echo '</div>'; But the above code doesnt seem to be stripping out links. Can anyone see what i am doing wrong. Kind Regards Dean im not sure how to do this: my code used the deprecated ereg_replace like this: $name = ereg_replace(" ", "", $name); i replaced it with this: $name = preg_replace(" ", "", $name); //this is line 65 but know get a warning: Warning: preg_replace(): Empty regular expression on line 65 what am i doing wrong please? thank you Hi there! I'm trying to use preg_replace to include/get content a php file with dynamic variable $txt = "Bla bla bla bla bla [gls]12[/gls] bla bla bla"; $txt = preg_replace("#\[gls\](.*?)\[/gls\]#si",file_get_contents('shipping.php?id=\\1'),$txt); ..and it doesn't work. I can not get ID. When I do like that $txt = preg_replace("#\[gls\](.*?)\[/gls\]#si","My Id is - \\1"),$txt); i see it, but not when I'm using file_get_contents function. Thanks in advance for help. Hey, I am trying to write a sort of replace function where {user:1} is converted into a set of information from a database using preg_replace. So I have created the following preg_replace: $patterns = array(); $patterns[1] = '/\{user:([0-9]+)\}/'; $replacements = array(); $replacements[1] = UserCredit('$1'); ksort($patterns); ksort($replacements); $content_show = preg_replace($patterns, $replacements, $content); echo $content_show; and then the function UserCredit which is just above that on the page: function UserCredit($user_id) { require 'connect.php'; $user_lookup = mysql_query("SELECT * FROM prop_users WHERE ID = '$user_id'"); while($row = mysql_fetch_array($user_lookup)) { $user_name = $row['full_name']; $user_file = $row['credit_file']; $user_url = $row['link_url']; $user_url_alt = $row['link_alt']; } if(mysql_num_rows($user_lookup)==0){ die("An error has occured, Sorry for any Inconvenience Caused! User ID=".$user_id); } require 'close.php'; $output = "<li class=\"clearfix\"><div class=\"credimg\"><img src=\"".$user_file."\" width=\"75px\" height=\"75px\" alt=\"".$user_name."\" /></div><p><strong><a href=\"".$user_url."\" title=\"".$user_url_alt."\">".$user_name."</a></strong><br /><em>Writing / Code</em></p></li>"; return $output; } I have tested the function and it works when I echo UserCredit('1'); And the preg_replace works at simply extracting the value for the user. The only problem is that I can't figure a way for the preg_replace to carry the value of $1 into the function it just literally carries "$1" Is there something obvious that I am missing or am I going around this the wrong way? What I'm trying to do is replace message_tags to links and web addresses to links in facebook json data, the problem is I need to do these in 2 steps, first do the message tags then then web addresses. The message_tags need to be done first because they rely on offset and length of the message to do the replace.
To code to first replace the message_tags is
// convert message tags to links if($post->message_tags) { usort($post->message_tags, 'mySort'); foreach($post->message_tags as $message_tag) { foreach($message_tag as $link) { $post_text = substr_replace($post_text,"<a href=\"https://www.facebook.com/".$link->id."\" target=\"_blank\">".$link->name."</a>",$link->offset,$link->length); } } }The second part is to convert any web address (http://example.com) to a link (<a href ="http://example.com" title="http://example.com" target="_blank">http://example.com</a>) // check if any entites exist and if so, replace them with hyperlinked versions $post_text = preg_replace("/((http:\/\/|https:\/\/)[^ )]+)/e", "'<a href=\"$1\" title=\"$1\" target=\"_blank\">'. ((strlen('$1')>=250 ? substr('$1',0,250).'...':'$1')).'</a>'", $post_text);So now to the problem, the second part now rewrites the first part and completely destroys the link because it just looks for http: and https: so in my preg_replace I need a way to not include anything that has already been converted, so not include anything that already has href="http Hi guys, this is what im trying to achieve, im trying to remove the links to the current page for all my web pages with a single script, like for example, if i am on a home page, then the home page hyperlink is disabled, and if i am on a about us page the hyperlink for about us is disabled, i have went through many forums and posts but the more i read the more confused i get, however i came up with a method of my own with many bits of code i've read about everywhere. firstly, the script collects the current url of the page(i dont need https or any sub direct, its just for example index.php) then it stores the page name, index.php into a variable say - $current Then a menu.php which is the navigation file is loaded and heres the part i cant get to work, i would like to match find that <a href="index.php"> index </a> and replace that with <a> index </> so that the link to the current page is disabled..i tried using preg_replace but failed miserably and opted to use str_replace Heres the code: <?php //the function below gets the current page url or file //i got this off webcheatsheet - http://www.webcheatsheet.com/PHP/get_current_page_url.php function getPage(){ return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1); } //store the current url or the file name into a variable $current = getPage(); echo "The current page name is ".$current; $menu=file_get_contents("menu.php"); $string = "<a>"+$current+"</a>"; $link = "<a href="+ current +"> "; //i could not get preg_replace to work or come up with an effective regular expression //which is why i use str_replace instead. $menu=str_replace("" , $string, $menu); echo $menu; ?> Hi all. I'm having a bit of an issue with preg_replace and I'm not sure how to solve it. I'm having preg_replace find a "bus" (short for business) prefix in a file name and then print "Business Management" instead of "bus". I also have another prefix of "bus_it_requests". When preg_replace finds that prefix, it prints "Business Management It Requests" but I need it to print "Business It Requests". How do I have preg_replace make the distinction in the prefix matches? Okay, i know there are lots of these questions out there and tons of ways to do this. I do have it working to replace URLs with <a href="URLs">URLs</a> but now i need some help detecting <a href=""></a> so the preg_replace will not double up the code and mess up the link. here is my code so far. Code: [Select] <?php echo 'COMMENTS<br /><br />'; while($rows = mysql_fetch_array($comres)){ list($comid, $menu_title, $post_id, $comdate, $comname, $comment) = $rows; $comment = nl2br($comment); $comment = preg_replace('/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/', '<a href="$0" target="_new">$0</a>', $comment); $comment = str_replace("´", "'", $comment); $comdate = date("g:ia - M j, Y",strtotime("$comdate")); echo 'User: '.$comname.'<font color="#B20303"> - '.$comdate.'</font><br />'; echo ' - '.$comment.'<br /><br />'; } ?> so with this code, if someone commented a link like this http://example.com it will turn it this <a href="http://example.com" target="_new">http://example.com</a> but now if someone knew some basic HTML coding, and they used this <a href="http://example.com">Example.com</a> it will look like this... <a href=<a href="http://example.com" target="_new">http://example.com</a>>Example.com</a> |