Jump to content

Search the Community

Showing results for tags 'preg_replace'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


  • Welcome to PHP Freaks
    • Announcements
    • Introductions
  • PHP Coding
    • PHP Coding Help
    • Regex Help
    • Third Party Scripts
    • FAQ/Code Snippet Repository
  • SQL / Database
    • MySQL Help
    • PostgreSQL
    • Microsoft SQL - MSSQL
    • Other RDBMS and SQL dialects
  • Client Side
    • HTML Help
    • CSS Help
    • Javascript Help
    • Other
  • Applications and Frameworks
    • Applications
    • Frameworks
    • Other Libraries
  • Web Server Administration
    • PHP Installation and Configuration
    • Linux
    • Apache HTTP Server
    • Microsoft IIS
    • Other Web Server Software
  • Other
    • Application Design
    • Other Programming Languages
    • Editor Help (PhpStorm, VS Code, etc)
    • Website Critique
    • Beta Test Your Stuff!
  • Freelance, Contracts, Employment, etc.
    • Services Offered
    • Job Offerings
  • General Discussion
    • PHPFreaks.com Website Feedback
    • Miscellaneous

Find results in...

Find results that contain...

Date Created

  • Start


Last Updated

  • Start


Filter by number of...


  • Start





Website URL








Donation Link

Found 16 results

  1. Can someone please tell me why neither one of these are working: $content = str_replace(array("\r", "\n"), '', $row['content']); $content = preg_replace( "/\r|\n/", '', $row['content']); I've tried each one of the to replace $content = $row['content']; and for some ungodly reason neither one of them are working. This is simply pulling content from a database but it keeps displaying \r\n when displayed in a page. Why?
  2. I am new to PHP, ereg_replace is deprecated so need to convert ereg_replace to preg_replace, I can't figure out how to put "delimiters and escape character" on $TEST_FILE below, can someone help me on this. function KernelBase(){ $i=0; $TEST_FILE="fdKernel/Init.php"; $TEST=$TEST_FILE; while(!file_exists($TEST)){ $TEST="../".$TEST; if($i>15) break; $i++; } return trim(ereg_replace($TEST_FILE,NULL,$TEST))."fdKernel/"; }
  3. Hi, I'm using TCPDF to create a PDF from a webpage. This works great but I have the following problem: I'm building in WordPress and as you may know WP saves images on different sizes. (e.g. 150x150, 300x300 and 1024x1024) The thing I'm trying to create is that users can download a PDF ready for printing. So High Resolution images. I use a preg_match_all function to match the image source and remove the additional image size from the filename so the original high resolution image is loaded. e.g. image-150x150.jpg will be replaced by image.jpg. So now the original image that has a image size of 6024x6024px and 300 DPI is loaded. Then TCPDF uses a resize function that resizes the image back to 150x150 px but sets the DPI to 300 so it can be printed on a good quality. This all works but now the problem comes: If I do the preg_match_all function the image gets "pulled" from the rest of the content and looses it's position. So my thought to fix this was. Get a preg_replace function and replace the image tag with a function that executes the whole image change like described above. This doesn't work...... So i'm stuck here. Hope anyone can assist me with this. Here is what I have done so far. function execute(){ preg_match_all("/<img[^>]+src=(?:\"|\')\K(.[^\">]+?)(?=\"|\')/",$content,$matches,PREG_PATTERN_ORDER); for( $i=0; isset($matches[1]) && $i < count($matches[1]); $i++ ) { $search = array('-'.$s1.'x'.$s2.'', '-'.$s3.'x'.$s4.'', '-'.$s5.'x'.$s6.''); $replace = array('', '', ''); $a = $matches[1][$i]; if (strpos($a,'-'.$s1.'x'.$s2.'') !== false) { $w = $s1; $w = $pdf->pixelsToUnits($w); $h = $s2; $h = $pdf->pixelsToUnits($h); $l = '57'; $l = $pdf->pixelsToUnits($l); $t = '170'; $t = $pdf->pixelsToUnits($t); }elseif (strpos($a,'-'.$s3.'x'.$s4.'') !== false) { $w = $s3; $w = $pdf->pixelsToUnits($w); $h = $s4; $h = $pdf->pixelsToUnits($h); $l = '217'; $l = $pdf->pixelsToUnits($l); $t = '20'; $t = $pdf->pixelsToUnits($t); }elseif (strpos($a,'-'.$s5.'x'.$s6.'') !== false) { $w = $s5; $w = $pdf->pixelsToUnits($w); $h = $s6; $h = $pdf->pixelsToUnits($h); $l = '57'; $l = $pdf->pixelsToUnits($l); $t = '310'; $t = $pdf->pixelsToUnits($t); } $content .= $pdf->Image(str_replace($search,$replace,$matches[1][$i]), $l, $t, $w, $h, 'JPG', '', '', true, 300, '', false, false, 0, false, false, false); } }; global $post; $id = $_GET['id']; $content_post = get_post($id);// get the content $content = $content_post->post_content; $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); $content = preg_replace('/<img[^>]+./e',execute(), $content); // replace the image tag with the new image // EXPORT IT ALL $pdf->writeHTML($content, true, false, true, false, '');
  4. Hello Every one Please see my code <?php include ("includes/config.php"); $afurl = $_SERVER['REQUEST_URI']; $afurl = preg_replace('/\/+/', '', $afurl); $afurl = preg_replace('/\;+/', '', $afurl); $afurl = preg_replace('/\&+/', '', $afurl); $afurl = preg_replace('/\#+/', '', $afurl); $afurl = preg_replace('/\|+/', '', $afurl); $afurl = preg_replace('/\@+/', '', $afurl); $afurl = preg_replace('/\%5B+/', '', $afurl); $afurl = preg_replace('/\%5D+/', '', $afurl); $afurl = preg_replace('/\%27+/', '', $afurl); $afurl = preg_replace('/\%C2+/', '', $afurl); $afurl = preg_replace('/\%BB+/', '', $afurl); $afurl = preg_replace('/quot+/', '', $afurl); $afurl = preg_replace('/\%E2+/', '', $afurl); $afurl = preg_replace('/\%80+/', '', $afurl); $afurl = preg_replace('/\%93+/', '', $afurl); $afurl = preg_replace('/\$+/', 'c', $afurl); $afurl = preg_replace('/\"+/', '', $afurl); $afurl = preg_replace('/\?+/', '', $afurl); $afurl = preg_replace('/\.html+/', '', $afurl); $afurl = preg_replace('/\-+/', ' ', $afurl); $queryArray = explode(" ", $afurl); for ($i=0; $i< count($queryArray); $i++) { $keyworddd = mysql_real_escape_string($queryArray[$i]).","; echo $keyworddd; } ?> I Want Set $keyworddd at here: <meta name="keywords" content="$keyworddd"> Can anyone tell me how to do this please?
  5. 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!)
  6. 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
  7. Hello Guys, Need a small help, as this is complex for me. With PHP 5.4, our server throws "Deprecated: Function eregi_replace() is deprecated" for a file and need you experts help to convert below 3 lines and replace it with preg_replace function. Can any one help with new lines of below code : $body_html = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $body_html); $body_html = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="http://\\2">\\2</a>', $body_html); $body_html = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', '<a href="mailto:\\1">\\1</a>', $body_html); Thank you & Appreciate it.
  8. I am change the PHP 5.2 to PHP 5.3 and trying to use preg_match replace ereg_replace. But I keep getting the error Warning: preg_replace() [function.preg-replace]: No ending delimiter '_' in code function Relink($linkstrip) { $linkstrip = ereg_replace('_+', '-', str_replace(array(' ', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '+', '|', '\\', '\'', '"', '[', ']', '{', '}', ':', ';', '.', ',', '/', '?', '', '<', '>'), '_', trim(ereg_replace('[[:space:]]+', ' ', trim($linkstrip))))); Can anyone please help me? Thank you Best Regards, Pom
  9. <!DOCTYPE html> <?php $request = 'http://api.wolframalpha.com/v2/query?appid=YKAERX-QJJAE7L2G6&input=albert%20einstein&format=html'; $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_URL, $request); $response = curl_exec($curl); curl_close($curl); $blank = ''; $remove = ']]>'; $display = preg_replace($remove, $blank, $response); ?> <html> <head> <style> div.ex { width:500px; padding:10px; border:5px solid gray; margin:0px; } </style> </head> <body> <div class="ex"> <?php echo $display ?> </div> </body> </html>
  10. hi all, i am a newbie member of php world, i need to learn the answer of a simple question why "preg_replace('/\@!$@&()\'*+,;=%]+)"\](.+)\[\!url\]/', '<a href="$1">$2</a>', $string)" expression adds my own site address at the beginning of the link created? such as http://www.mysite.com/www.thesitepreg_replace_created.com
  11. I've been working on my own templating feature in which a page of static html is replaced by php variables when they exist. When $ (dollar sign) exists on the static page before the templating magic it is getting omitted when i replace things using preg_replace() for the output. This simple example illustrates this: $price = '$10.99'; $template = 'Your cost: $price'; $output = preg_replace('/\$price/', $price, $template); echo $output; //Your cost: .99 notice the "$" is now missing from output. Anyone know how to solve this problem and preserve dollar signs in an elegant way. I know I can do something like $price = str_replace('$', '\$', $price); but that seems ugly and simply another string function that isn't necessary. (I'm already using a lot of resources to make the templating feature work) Note: I'm using php ver. 5.2 I thought maybe preg_replace_callback() might work but it doesn't seem to be supported
  12. Hi, I'm using a preg_replace to change [menu] into Menu::makeMenu(); , but when it executes the replace, for some reason it's sending my code to the first line after the <body> tag rather than where the [menu] was in the first place. Preg Replace: function formatPage($transform){ $a = array( "/\[element=\"(.*?)\"\]/ise", "/\ /ise", "/\[menu]/ise", "/\[login-button]/ise", ); $b = array( "elementCreate(\"$1\");", "pageCreate();", "Menu::makeMenu();", "loginBtn();", ); $transform = preg_replace($a, $b, $transform); return $transform; } I can copy out the whole function and stuff for the Menu:: but I don't think it'll matter. Please help! Cheers.
  13. Hi hopefully someone can help, i'm having a slight panic on a project I'm working on. I've got 90% of this all done but am having a few concerns regarding some special characters. Heres' the situation. I have a database table i need to export to a CSV (so the client can then import into their own system) I've got this working a dream, downloads the file and all that perfectly. Now i have a few fields in this table where we're going to get special characters (double quotes, single quotes etc) just the kind of thing that will bust a CSV from importing (or opening in excel/similar properly). Basically what i could do with knowing is should i just clean the strings of the bad characters? possibly loosing some of the meaning of the data (for instance one field is a message, and this is likely to contain all sorts of characters), or is there something else i can do that will retain the data - i'm thinking the brute force clean could be fine, but would like to know my options. Also could do with a hand with regards to cleaning the string, I've had a look at preg_replace, example here: $fnameClean = preg_replace("/'/", "", $fnameDirty); Really hope this is a simple one guys, thanks in advance.
  14. Hi, I have created an array with symbols that I'd like to allow in a string of text. $symbols[] = '>'; // Greater Than or Open Angle Bracket $symbols[] = '<'; // Less Than or Close Angle Bracket $symbols[] = '/'; // Forward Slash $symbols[] = '\\'; // Back Slash $symbols[] = '&'; // Ampersand $symbols[] = '£'; // Pound Sterling $symbols[] = '$'; // Dollar $symbols[] = '\"'; // Quotation Marks $symbols[] = '\''; // Apostrophe $symbols[] = '#'; // Hash $symbols[] = ' '; // Space $symbols[] = '.'; // Period I would now like to use something like str_replace or preg_replace (whichever is best) to replace any character that is not included within the array $symbols with "". I know there are other ways to filter characters but it isn't a suitable solution considering who will be maintaining it... :-\ I'm hoping for something like; but obviously a working way. $myText = str_replace(!$symbols, "", $myText; $myText = preg_replace(!$symbols, "", $myText; Thank you for your help.
  15. I have a function with a button, that fetches a javascript pop-up with an input where people can post a hyperlink to another website. That works, but now, I want to give the hyperlink a different name, other than the linkname itself. This is what I want to happen: http://i.stack.imgur.com/lcMgH.png So you can see that 1 2 and 3 are links. On my site, you can see the links too, but the full http:// path of the link is shown and not a custom name. How can I make this happen?
  16. I am trying to format form text input for use in a mysql fulltext boolean mode search. If someone wants to match "all" words in the input box, I need to put a "+" sign in front of every word. If they choose to match "any" words, we can ignore this step. If they enter a phrase -- indicated by wrapping the phrase in quotes (eg., "search me") I need to include that quoted string as is. Easier to see with an example: Input: one two "test test" Assuming someone has asked to get a result matching "all" words/phrases, I should format this input like so: +one +two "test test". Here's my code to do this formatting. It works great up until, of course, the last step. I end up with this output: +one +two +"test +test". The + inside of the quoted phrase -- "test +test" -- is the problem. It seems preg_replace() is just skipping over doing the replacing I need. Anyone see anything weird here? $keywords = 'one two "test test"'; // Checking that the $keywords string doesn't start with a quotation mark. If not - this is a word, add a + if (substr($keywords, 0,1) != '"') { $keywords = '+' . $keywords; } // Rewriting $keywords so that every space in the string now includes a + -- will use this in a sec to replace the quoted phrase with "+" with the original quoted phrase - no + $prep_string = str_replace(' ', ' +', $keywords); // now $keywords = +one +two +"test +test" // Finding any quoted phrases in the original $keywords string here $pattern = "/\"[^\"]*\"/"; preg_match_all($pattern, $keywords, $matches); // the match = "test test" if (!empty($matches[0])) // $matches[0] is the returned array that holds any matches found in the original $keywords string { $prepped = array(); foreach ($matches[0] AS $value) { // Looping through and preparing each value (here the value is just "test test") to include a + // This is so that I have a way to match values in the $prep_string string $prepped[] = '/' . str_replace(' ', ' +', $value) . '/'; // this outputs '/"test +test"/' } $new_string = preg_replace($prepped, $matches[0], $prep_string); $new_string = str_replace('+"', ' "', $new_string); // The rest here is just to dump all of this to screen, you'll see that preg_replace didn't replace "test +test" found in $prepped with "test test" found in $matches[0] 'original matches: ' . var_dump($matches[0]); echo '<br />'; 'prepped matches: ' . var_dump($prepped); echo '<br />'; echo 'preg_replace output: ' . $new_string; // this is } If you var_dump matches[0] and $prepped, and then echo $new_string, here's what you see: original matches: array(1) { [0]=> string(11) ""test test"" } prepped matches: array(1) { [0]=> string(14) "/"test +test"/" } preg_replace output: +one +two +"test +test" ====== this should be +one +two "test test"
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.