PHP - Preg_replace And The Pipe Character
Similar TutorialsJust as the title says. In cPanel, I have it al configured just fine; the script recieves the email, and I do receieve an email back, but here's where I come into problems. It simply will NOT save the attachments to the server, I've tried loads of different implementations I've found, and nothing's working. pipe script: #!/usr/local/bin/php <?php // Need PEAR installed include('Mail.php'); include('Mail/mime.php'); require_once 'Mail/mimeDecode.php'; // read email using stdin $fx = fopen("php://stdin", "r"); $email = ""; while (!feof($fx)) { $email .= fread($fx,1024); } $params['include_bodies'] = true; $params['decode_bodies'] = true; $params['decode_headers'] = true; $params['input'] = $email; /* $message=new Mail_mimeDecode(); $mailObj=$message->decode($params); */ $mailObj = Mail_mimeDecode::decode($params); // Who is it from $from=$mailObj->headers['from']; // Get Subject $subj=$mailObj->headers['subject']; // Get Message Body $body=$mailObj->parts[0]->body; $gather="From:$from\nSubject:$subj\nBody:$body"; // Get and Save the Attachments foreach($mailObj->parts as $pp){ if ($pp->disposition=='attachment'){ $tmp=$pp->d_parameters['filename']; if($tmp !== ""){ $open = fopen($tmp, 'w'); if($open){ $write = fwrite($open,$pp->body); if(!$write){ $err = "Cannot write to file: '".$tmp."' - var: ".$write; } }else{ $err = "Cannot create/open file: '".$tmp."' - var: ".$open; } } } } $tox = $from; $subjectx = 'Your Photo was Uploaded!'; $messagex = $err; $headersx = 'From: piculo.us Uploader <upload@piculo.us>' . "\r\n" . 'Reply-To: upload@piculo.us' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($tox, $subjectx, $messagex, $headersx); exit; ?> Here's the email I get back: Cannot create/open file: '2879_1150712806251_1180540351_425140_7940412_n.jpg' - var: as you can see, it is aware that an attachment exists, but it won't create the file, and fopen() doesn't say anything. Any ideas?? Hey all, Let's say I want to do something like this: Code: [Select] <?php function exists($a){ $b ||= $a; } echo exists(1); ?> Basically, every time exists is called, it will only assign b to a if b isn't already initialized. What's the most effective way to achieve the equivalent in php? ternary? Thanks for response. 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, 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'])); } 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. 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'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
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? & 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 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 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'm working with the youtube API and the function Code: [Select] $relatedVideo->watchURL returns a video url However when I use Code: [Select] preg_replace("youtube","test",$relatedVideo->watchURL); it gives me the following error Code: [Select] preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: Delimiter must not be alphanumeric or backslash in <b>C:\wamp\www\site\test.php</b> on line <b>317 Help please? 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. This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=350441.0 I have no idea what the problem is with preg_replace and strings smaller than 3 characters. I'm writing to do a pattern search of abreviations(array) and replacements (array)... tried word boundries, which seems not to work at all. (it is VITAL that im able to match the whole word) here is a quick example which I cannot understand maybe someone could explain to me: 1. why repitions of certain abrevs do not get replaced. 2. why '/\bs.\b/' wont get matched (where there is a space before the b. - hense the word boundry) here is a snippet of code could someone please explain why this happens: Code: [Select] <?php $string='de d. s. pres. div. de de d. s.'; //DEFINE PATTERN $patterns = array(); $patterns[0] = '/ s. /'; $patterns[1] = '/ b. /'; $patterns[2] = '/ pres./'; $patterns[3] = '/ Ind./'; $patterns[4] = '/ Mar./'; $patterns[5] = '/ Agt./'; $patterns[6] = '/ Agy./'; $patterns[7] = '/ Indpls./'; $patterns[8] = '/ Chgo./'; $patterns[9] = '/ Corp./'; $patterns[10] = '/ U./'; $patterns[11] = '/ Fin./'; $patterns[12] = '/ ls./'; $patterns[13] = '/ Pres. /'; $patterns[14] = '/ Internat. /'; $patterns[15] = '/ m. /'; $patterns[16] = '/ N.Y.C./'; $patterns[17] = '/ div. /'; $patterns[18] = '/ de /'; $patterns[19] = '/ d. /'; //DEFINE PATTERN REPLACE $replacements = array(); $replacements[0] = ' son of '; $replacements[1] = ' born in '; $replacements[2] = ' president'; $replacements[3] = ' Indiana '; $replacements[4] = ' March '; $replacements[5] = ' Agent '; $replacements[6] = ' Agency '; $replacements[7] = ' Indianapolis '; $replacements[8] = ' Chicago'; $replacements[9] = ' Corporation '; $replacements[10] = ' U '; $replacements[11] = ' Financial '; $replacements[12] = ' Island'; $replacements[13] = ' President '; $replacements[14] = ' International '; $replacements[15] = ' married to '; $replacements[16] = ' New York City '; $replacements[17] = ' divorced'; $replacements[18] = ' de '; $replacements[19] = ' daughter of '; echo '------ Original ---------<br>'.$string.'<br>'; $data = preg_replace($patterns, $replacements, $string); echo '<br>------ Abbreviation Replace ---------<br>'.$data.'<br>'; ?> OUTPUT: ------ Original --------- de d. s. pres. div. de de d. s. ------ Abbreviation Replace --------- de daughter of son of president divorcedde daughter of d. s. any help is appriciated thanks. 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? Code: [Select] $str = preg_replace('/('.$word.')/i', '<em>\1</em>', $str); how come this doesn't work when $word = 'http://www.google.com', or any other URL? hi all why this code doesn't work? $string = "some words here, 12654124"; $pieces = explode(",", $string); for ($i=0; $i<=1; $i++){ $pieces[0] = preg_replace("/[^A-Za-z ]$/", "text to be replace", $pieces[0]); echo $pieces[0] . "<br>"; } It returns: Code: [Select] some words here some words here The right result would be: "text to be replace, 12654124" |