PHP - Preg Replace
basically what i am trying to do is replace the
<a href="whatever.com"> with <a href="whatever.com" onclick = "if (! confirm("Continue?")) return false;"> so that when the user clicks the url in a message the box pops up first then redirects them to the url if they click confirm. However i am getting the following error: Code: [Select] Warning: preg_replace() [function.preg-replace]: Compilation failed: reference to non-existent subpattern at offset 11 here is the code i used. $message_content = $content_info['message_content']; $pattern='<a href="\\1">'; $replace='<a href="\\1" onclick = "if (! confirm("Continue?")) return false;">'; $message_content = preg_replace($pattern, $replace, $message_content); echo $message_content; Anyone know where i went wrong? Similar TutorialsSo i have this script that outputs Listed Files inside a folder echo '<option value="'.$fileName.'">'.$fileName.'</option>';} like this : <option value="Fonts/28 Days Later.ttf">Fonts/28 Days Later.ttf</option> etc files .... i would like to know how may i remove " Fonts/ " And the .ttf extension from the second part that repeats (the name) using something like preg_replace and to make the first letter uppercase using ucfirst() And if someone is familiar with preg_replace could you please give me a tutorial on how it works to replace some text ? Thanks In Advance $name = preg_replace('{(\d+)}', convertNumber("$1"), $row['name']); why isnt this working? im having trouble using this function. im trying to change the strings within a post. eg: Code: [Select] This is an example post [attachment=file.png] what im trying to do is change [attachment=file.png] to file.png. Here is the code i have but it doesnt change anything. $p_content = str_replace('<br>', '', $post_info[$key]['p_content']); $attach = preg_split('|\[attachment=|', $p_content); for($i=0; $i<count($attach); $i++) { $attach[$i] = str_replace(']', '', $attach[$i]); if(file_exists($config['asf_root'].'attachments/'.$attach[$i])) { $p_content = preg_replace('|\[attachment=([0-9a-zA-Z\.-_])\]|#i', $attach[$i], $p_content); } } echo $p_content; it first gets all the attachment tags within the post then applies the preg replace to all of them. But the preg replace isnt replacing, so i must have gone wrong somewhere. Any tips/ideas? Thanks Hi, I am having a pretty difficult time getting files to upload to my server that have a Single Quote in the file name. Example: myfile's.pdf I need to strip the file name of the quote before it uploads to the server. I currently am using SWFUpload and this is the bit of code that was included with it. I need to modify it to remove the single quotes as well. Been hammering my head over this all day. $file_name = preg_replace('/[^'.$valid_chars_regex.']|\.+$/i', "", basename($_FILES[$upload_name]['name'])); if (strlen($file_name) == 0 || strlen($file_name) > $MAX_FILENAME_LENGTH) { HandleError("Invalid file name"); exit(0); } thanks Code: [Select] $text = preg_replace('{-(\d+)}e', '-convertNumber("$1")', $text); i have strings im trying to replace like this: name-2 > name-two name-p2 > name-p2 im trying to match and replace the first one and basically ignore the second one. ive included the hyphen in the code. so why isnt this working? Code: [Select] line 129- function filterBadWords($str) { $result = mysql_query("SELECT badwords FROM clean") or die(mysql_error()); $replacements = "*"; while($row = mysql_fetch_assoc($result)) { $bad_words= $row['badwords']; $spaced_bad_words = preg_replace("/(.)/i", "\${1} ", $bad_words); $spaced_bad_words = trim($spaced_bad_words); $str = preg_replace("/$bad_words | $spaced_bad_words/i", str_repeat('*', strlen($bad_words)), $str); } return $str; Line 143 - } I go this error "Warning: preg_replace() [function.preg-replace]: Compilation failed: unmatched parentheses at offset 1 in C:\xampp\htdocs\login\chatlog3.php on line 139" I've never really made my own bbcode parser function before, but I'm going off of different tutorials to help me create my own style of parsing. I've actually never really used preg_replace much, either. My code is returning an odd error and I have no idea how to fix it. I'm using codeigniter and the error it returns is below: Quote preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 6 This is the code for my function: Code: [Select] function bbParse($string){ $codes = array( '/\[b\](*?)\[\/b\]/' => '<b>\\1</b>', //bold tags '/\[h2\](*?)\[/h2\]/' => '<h2>\\1</h2>', //h2 tags '/\[h3\](*?)[\/h3\]/' => '<h3>\\1</h3>', //h3 tags '/\[quote\](*?)\[\/quote\]/' => '<blockquote>\\1</blockquote>', //quote tags using the template's blockquote tag '/\[img\](*?)\[/img\]/' => '<img src=\'\\1\' alt=\'Image Not Available\'>', //image tags '/\[url\=(*?)\](*?)\[/url\]/' => '<a href=\'\\1\' title=\'\\2\'>\\2</a>', //anchor tags '/\[p\](*?)\[/p\]/' => '<p>\\1</p>' //paragraph tags ); $string = preg_replace(array_keys($codes), array_values($codes), $string); return $string; } This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=351288.0 i keep getting this at top of page Warning: preg_replace() [function.preg-replace]: No ending delimiter '`' found in C:\xampp\htdocs\socialtscripts\activation.php on line 16 here is the code that it is reffering to Code: [Select] preg_replace("`", "", $id); This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=321641.0 i get the following error in the comments in wordpress blog..: Warning: preg_replace() [function.preg-replace]: No ending delimiter '~' found in /home/doughhhh/public_html/wp-content/themes/upload/includes/theme-comments.php on line 75 the content of the file is: Code: [Select] <?php // Fist full of comments if (!function_exists("custom_comment")) { function custom_comment($comment, $args, $depth) { $GLOBALS['comment'] = $comment; ?> <li <?php comment_class(); ?>> <a name="comment-<?php comment_ID() ?>"></a> <div id="li-comment-<?php comment_ID() ?>" class="comment-container"> <div class="comment-head"> <?php if(get_comment_type() == "comment"){ ?> <div class="avatar"><?php the_commenter_avatar($args) ?></div> <?php } ?> <div class="reply"> <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?> </div><!-- /.reply --> </div><!-- /.comment-head --> <div class="comment-entry" id="comment-<?php comment_ID(); ?>"> <span class="arrow"></span> <div class="comment-info"> <div class="left"><span class="name"><?php the_commenter_link() ?></span></div> <div class="right"> <span class="date"><?php echo get_comment_date(get_option( 'date_format' )) ?> <?php _e('at', 'themejunkie'); ?> <?php echo get_comment_time(get_option( 'time_format' )); ?></span> <span class="perma"><a href="<?php echo get_comment_link(); ?>" title="<?php _e('Direct link to this comment', 'themejunkie'); ?>">#</a></span> <span class="edit"><?php edit_comment_link(__('Edit', 'themejunkie'), '', ''); ?></span> </div> <div class="clear"></div> </div> <?php comment_text() ?> <?php if ($comment->comment_approved == '0') { ?> <p class='unapproved'><?php _e('Your comment is awaiting moderation.', 'themejunkie'); ?></p> <?php } ?> </div><!-- /comment-entry --> </div><!-- /.comment-container --> <?php } } // PINGBACK / TRACKBACK OUTPUT if (!function_exists("list_pings")) { function list_pings($comment, $args, $depth) { $GLOBALS['comment'] = $comment; ?> <li id="comment-<?php comment_ID(); ?>"> <span class="author"><?php comment_author_link(); ?></span> - <span class="date"><?php echo get_comment_date(get_option( 'date_format' )) ?></span> <span class="pingcontent"><?php comment_text() ?></span> <?php } } if (!function_exists("the_commenter_link")) { function the_commenter_link() { $commenter = get_comment_author_link(); if ( preg_match( '~]* class=[^>]+>~', $commenter ) ) {$commenter = preg_replace( '~(]* class=[\'"]?)~', '\\1url ' , $commenter ); } else { $commenter = preg_replace( '~(<a )/', '\\1class="url "~' , $commenter );} echo $commenter ; } } if (!function_exists("the_commenter_avatar")) { function the_commenter_avatar($args) { $email = get_comment_author_email(); $avatar = str_replace( "class='avatar", "class='photo avatar", get_avatar( "$email", $args['avatar_size']) ); echo $avatar; } } ?> help! [attachment deleted by admin] The situation is that I have a large string with some identifiers to say "replace me" with something else. My goal is to replace the values, but if one replacement includes one of the other keys, I don't want to replace it.
For example:
<?php $str = 'some blob ~abc~ followed by ~def~'; $arr_orig = array( '~abc~', '~def~' ); $arr_new = array( 'value~def~', 'blah' ); echo str_replace( $arr_orig, $arr_new, $str ); ?>This will echo: some blob valueblah followed by blah But that's not what I'm trying to accomplish. Basically, I want ~abc~ to be replaced with the literal: value~def~ and for the ~def~ that happens to be part of that string to NOT be replaced. I hope what I'm asking is clear. Basically, preg_replace/str_replace with arrays seems no different than just doing a FOR loop and replacing ~abc~ and then ~def~. But that's not what I need. I'm hoping that if ~abc~ is replaced with text that happens to be another identifier -- ~def~, ~xyz~, whatever -- that this already replaced text is not again replaced. Is there a built-in function to do something like this? If not, should I just parse the whole string and count characters or something? Seems like a pain (and slow!) Hi i wrote a find and replace code but my code replace last value of array and skip the first, second.... values in the array. Please give me an idea because i stack with this code. Regards $KeyWord = explode("\n", $RowGetWords['KEYWORDS']); $Replace = explode("\n", $RowGetWords['ReplaceTo']); for($i=0; $i<count($KeyWord); $i++){ $pattern = $KeyWord[$i]; $replace = "<a href=\"" .$URL. "\" target=\"_blank\" >" .$Replace[$i]. "</a>"; $html = str_replace($pattern, $replace, $Row['MessageBody']); } This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=310802.0 Hello, I come from a C and PERL background. I seem to have trouble finding how to do this easily. What is the PERL equivalent of: $var =~ s/^\+//g; // remove a plus from the beginning, and from the entire string. Thanks. This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=311437.0 Hi, Say you had, <business_name type="string">L Choong</business_name>, contained inside a variable(content before and after this in the same var), how would you extract the 'L Choong' information as a new var? Thanks Hello, I am trying to crawl a website and get all the domain names listed on the site to output into my php script. I am using the following code.... The main area is the preg match area, since I know nothing about it, nor do I understand it a little bit! $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,"http://link I am getting data from.com); curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); $result=curl_exec ($ch); curl_close ($ch); // Search The Results From The Starting Site if( $result ) { // LOOKING FOR ANYTHING.COM all .com's preg_match_all( "(.*)\.com", $result, $output, PREG_SET_ORDER ); foreach( $output as $item ) { // ALL LINKS DISPLAY HERE print_r($item); } } Any help would be greatly appreciated! so I use file_get_contents to get the contents of a webpage, one of the lines has Code: [Select] <meta property="og:url" content="http://www.google.com/blahblahblah?s=5cd04d4a7632296b9cdb463d04e82c05" /> I want it to echo only http://www.google.com/blahblahblah so anything between <meta property="og:url" content=" and ?s=5cd04d4a7632296b9cdb463d04e82c05" /> I want to extract and put into a variable this works just fine but I was thinking preg match would be better $thread = explode('<meta property="og:url" content="', $psuc); $thread = explode('?s=', $thread[1]); echo $thread[0]; Thanks |