PHP - Preg_replace Hyphens
Hi, I've got a little problem at the moment with how to replace varying amounts of hyphens in a browser url.
scenario: I have a borwser url mydomain.com/location/foo-bar/ where "foo-bar" is actually "foo bar" in the database (no hyphen). I also have urls where the entry in the database does have the hyphens e.g. "foo-bar" Now im not quite sure how to go about writing these urls, and how to manipulate the GET values from them. what I've tried My first thought was to add a secondary hyphen to the url so that the database entires that equal "foo-bar" would be "foo--bar" in the browser url and the entries in the database that equal "foo bar" would have the single hyphen "foo-bar". Then i would use preg_replace to deal with the single hyphens by removing them and replacing with a space, and after that finding the double hyphens and replacing them with only one. In theory this should work (I think), but I've only recently got into using regex, this is what I've tried; $CNE = preg_replace("/[-]{1}/", " ", $CNE); $CNE = preg_replace("/[-]{2}/", "-", $CNE); so I've tired to match the single hyphen and then the double, but even before I tested this I didn't think it was going to work with this code. Can anyone correct me, or maybe suggest a better way to go about this issue. (note: using a different character to replace spaces is NOT an option in this case) Thanks Similar TutorialsFirstly I'm sure the answer is simple however due to my inexperience I am unable to figure it out!
I am executing a simple query to retrieve all ISBN numbers within my db and display these in a table.
In my db some of the ISBN numbers contain hyphens and some don't. I would like to display then in my table without hyphens.
I'm grateful of any assistance and advice.
I know I can remove all the hyphens from the db by executing a separate query however I'd like to strip them if possibly.
My Code;
$query = "SELECT * FROM book WHERE status != 'Archive' ORDER BY id"; while($row = mysqli_fetch_array($query)){ ?> <tr> <td><a href="itemView.php<?php echo '?isbn='.$row['isbn']; ?>" </a></td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> </tr> <?php } ?>J 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 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! 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. 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 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'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
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? 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? & Hi, I have a link like this: <a href="myword.php" title="myword">some other text myword blah foo</a> OK now I need to replace myword with newword, but only in the anchor text. So myword in the href or title should not be changed, only matches found inside the anchor text! I tried using str_replace but obviously this just matched all occurances. So I guess preg_replace is the way to go here but it is far too advanced for me and I cannot get my head around all the different symbols. Here is some example code: $html = '<a href="myword.php" title="myword">some other text myword blah foo</a>'; $new = 'newword' $html = preg_replace(//unsure here!); I just don't know how to work regex OT: Did you find it easy/difficult to learn? Thanks to any help 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. 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 i'm having trouble in combining the 2 preg_replace into 1. basically i have a string witch strange chars, first i remove all except alphanumeric chars and the remaining string has 1 or 2 spaces and i replace them with dash. Code: [Select] strtolower(preg_replace('/\s+/', '-', preg_replace("/[^0-9a-zA-Z]/i", ' ',$a['item']))); 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 had this code but now I need to change it to newer preg_replace function Can someone help me to do it please? Here is the code: $newString = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\" target=\"_blank\">\\0</a>", $originalString); Thanks in advance. $titleurl = preg_replace('/[^a-zA-Z0-9_ -%]/', '-', $d); $titleurl = preg_replace('/[ ]/', '-', $titleurl); How do you add to this to replace ---- with - --- with - -- with - and remove quotes? How do you remove a - if it's at the very end of $titleurl. For example, replace My-iPhone- with My-iPhone This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=350441.0 Quote 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 |