Jump to content

Nicklas

Members
  • Posts

    111
  • Joined

  • Last visited

    Never

Everything posted by Nicklas

  1. you dont need strlen, it´s enough with: [code=php:0]substr($postcode, 0, -3);[/code]
  2. The [b]e[b] modifier is missing in preg_replace() change to this: [code=php:0]echo preg_replace("/$searchterm/ise", 'highlight("\\0")',  $result);[/code]
  3. [quote]why you call the highlight function with highlight("\\0")?[/quote] \\0 is a backreference and holds the matched keyword(s) [quote]Because now the keywords in the result instead of highlight they get replaced with "highlight ("keywords")" string!!!![/quote] Show me your code.
  4. the preg_replace call the function highlight() and takes the match, split it and then surround each word with a <span> tag. Then the string is put back together via the $tmp variable and return it to preg_replace().
  5. do something like this if you want to highlight them individually: [CODE]<?php function highlight($str) { $tmp = ''; foreach(explode(' ', $str) as $word) $tmp .= "<span style=\"background-color:yellow\">$word</span> "; return trim($tmp); } $query = "test query"; $string = "hello there, this is a test query"; echo preg_replace("/$query/ise", 'highlight("\\0")', $string); ?>[/CODE]
  6. http://www.phpfreaks.com/forums/index.php/topic,115226.msg469086.html#msg469086
  7. better to use [ and  ], { and } will be out of use in PHP6
  8. [CODE]<?php $str = 'hello http://website1.com some word http://website2.com whatever else'; $spam = substr_count(strtolower($str), 'http://') - 1; echo $spam; // Result: 1 ?>[/CODE]
  9. [code=php:0]echo preg_replace('/(""").*?\1/s', '<span style="color: blue">\\0</span>', $text);[/code]
  10. correct me if i'm wrong, but if I remember correctly, the maximum allowed filesize is 2Mb. If your files go beond that, they wont be uploaded unless you edit your php.ini to allow bigger filesizes.
  11. How do you store this list? database, textfile... ?
  12. Here´s another way of displaying the first 10 words: [code=php:0]<?php $text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."; list($words) = explode(' |', preg_replace('/(.*?\s+){10}/s', '\\0|', $text)); echo $words; ?>[/code]
  13. I would use [url=http://www.php.net/preg_split]preg_split()[/url] to do the job. ex [code=php:0]list($artist, $title) = preg_split('/ - | \(/', $info);[/code] Then I just need to do something like this to loop thru my mp3 files and get the artist + title at the same time: [hr] [code=php:0]foreach(glob("some_dir/*.mp3") as $info) { list($artist, $title) = preg_split('/ - | \(/', $info); // Do what you want here... }[/code][hr]
  14. Take a look at [url=http://www.php.net/strtotime]strtotime()[/url] ex [CODE]<?php $date = '2007-12-01'; echo date("m-d-Y", strtotime($date)); ?>[/CODE]
  15. You could do something like this: [code=php:0]$str = 'bla bla bla...'; // select all <font> tags from the $str string preg_match_all('/(<font[^>]*>)/is', $str, $match); $match = $match[0]; // remove all tags with the size option foreach($match as $key => $tag) { if (preg_match('/size=("|\')?(\d+)?("|\')?/is', $tag)) unset($match[$key]); } // present all matched <font> tags print_r($match);[/code]
  16. You could do something like this [code]<?php // a callback-function used by preg_replace() function strong($match, $word) { // Replace all <> tags and bbcode [] stuff with | (pipe) chars $tmp = preg_replace('/(<|\[).*?(>|])(.*?(<|\[)\s*\/.*?(>|]))?/s', '|', $match); // Surround the target word with <strong> tags $tmp = preg_replace('/\b' . $word . '\b/is', '<strong>\\0</strong>', $tmp); // Match all <> tags and bbcode [] stuff from the un-edited $match var preg_match_all('/(<|\[).*?(>|])(.*?(<|\[)\s*\/.*?(>|]))?/s', $match, $tag); // Put all the tags back into $tmp by replacing the | chars with the tags stored in the $tag array foreach($tag[0] as $html) { $tmp = preg_replace('/\|/', $html, $tmp, 1); } // ... and return the string return $tmp; } // the word you want to replace $word = "text"; // ...and the string $str = 'bla bla text <p>here is some text <a href="text.php">text link</a> and a bit <i>more text</i> here</p>. End of text.'; // Replace all $word's using a callback-function $str = preg_replace('/(?<=<p>).*?\b' . $word . '\b.*?(?=<\/p>)/ise', 'strong("\\0", $word)', $str); // and the result is... echo $str; ?>[/code]
  17. There´s no need for regular expressions to do what you want, use explode() instead ex [code=php:0]$str = '15e-6812'; list( ,$str) = explode('-', $str);[/code] $str now holds the value 6812
  18. take a look at [url=http://www.php.net/strtotime]strtotime()[/url] ex [CODE]<?php $time = "12/19/06 04:27 am"; echo strtotime($time); ?>[/CODE]
  19. use [b]ffmpeg[/b] to convert between movie formats. If you dont know what it is, search the forum or just google it ;) simple syntax example [color=blue][b]ffmpeg -i original.avi newfile.3gp[/b][/color]
  20. As [b]kenrbnsn[/b] suggest, use [i]str_replace()[/i] instead. There´s no need for regular expressions.
  21. you can use glob() with the [i]GLOB_ONLYDIR[/i] parameter ex [code]<?php $path = "themes"; // path to the dir you want to scan foreach(glob("$path/*", GLOB_ONLYDIR) as $dir) { echo basename($dir) . "<br />\n"; } ?>[/code]
  22. Use explode() ex [hr] [code=php:0]$str = "Test Location:  United States - South Carolina - Greenville - Bellsouth.net In"; list( , $state, $city) = explode(' - ', $str);[/code] [hr] Now the state is stored in [b]$state[/b], and the city in [b]$city[/b] ;)
  23. You can do it with regular expressions. ex [code=php:0]echo preg_replace('~(.*?<br />.*?)<br />~s', '\\1', $str);[/code]
  24. ex [CODE]<?php $str = "audun is doing his homework;;http://www.audun.com/test.php;whenever he feels like it"; $array = preg_split('/;+/', $str); print_r($array); ?>[/CODE]
  25. here´s another way [CODE]<?php $reply = "Yes I am able to go to the pub tommorrow"; echo strlen(preg_replace('/[^a]/is', '', $reply)); ?>[/CODE]
×
×
  • 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.