Skip to main content

Posts

Showing posts with the label regex

Replace URLs in text with HTML links

Here is a design though: For example is I put a link such as http://example.com in textarea . How do I get PHP to detect it’s a http:// link and then print it as print "<a href='htttp://example.com'>http://example.com</a>"; I remember doing something like this before however, it was not fool proof it kept breaking for complex links. Another good idea would be if you have a link such as http://example.com/test.php?val1=bla&val2blablabla%20bla%20bla.bl fix it so it does print "<a href='http://example.com/test.php?val1=bla&val2=bla%20bla%20bla.bla'>"; print "http://example.com/test.php"; print "</a>"; This one is just an after thought.. stackoverflow could also probably use this as well :D Any Ideas Source: Tips4all

What"s wrong with this PHP Regex code?

The x modifier code in this tutorial Php regex tutorial gives me the following error: Warning: preg_match() [function.preg-match]: Unknown modifier ' ' in C:\xampp\htdocs\validation\test.php on line 16 Pattern not found What's wrong with it? <?php // create a string $string = 'sex'."\n".'at'."\n".'noon'."\n".'taxes'."\n"; // create our regex using comments and store the regex // in a variable to be used with preg_match $regex =" / # opening double quote ^ # caret means beginning of the string noon # the pattern to match /imx "; // look for a match if(preg_match($regex, $string)) { echo 'Pattern Found'; } else { echo 'Pattern not found'; } ?>

Java REGEX Operator precedence

I have a few strings: john doe happy george smith is happy Here's my Regular Expression: ([a-zA-Z ]+) (is happy|happy) I need group 1 to always be a name like "john doe" or "george smith" I need group 2 to always be either "happy" or "is happy" Right now, "george smith is happy" always matches up to "george smith is" and "happy" ... when I really want it to be "george smith" and "is happy" How do I make "is happy" have precedence over "happy"?

Javascript regex replace weird behavior

I'm developing a js to dig up Bible references on Portuguese web pages, and link them to a youversion.com url. This is one of those pages: http://www.adventistas.org.pt/Evangelismo/Web/Img/Downloads/Trimensario/2012/1t/online/licao7.htm This is the script: http://www.adventistas.org.pt/Evangelismo/Web/Img/Downloads/Trimensario/2012/1t/online/bibleref.js It pretty much works, but it acts kinda funny, and can't find out why. Sometimes it outputs empty links with correct bible references, and then outputs the real link, with a wrong reference. Eg: <a target="_blank" href="http://www.youversion.com/bible/verse/arc/gen/2/1-3" class="passagem"></a> <a target="_blank" href="http://www.youversion.com/bible/verse/arc/gen/2/1-3" class="passagem"></a> <a target="_blank" href="http://www.youversion.com/bible/verse/arc/gen/2/1-3" class="passagem"></a> <

How do I convert domain.com/foo/bar/baz/ into a string like "foo bar baz"?

Basically I want to be able to grab the ending of an url, and convert it into a string to be used somewhere. Currently I'm doing this (which is less than optimal): // grab the path, replace all the forward slashes with spaces local_path = location.pathname.toString().replace(/\//g,' '); // strip empty spaces from beginning / end of string local_path.replace(/^\s+|\s+$/g,"")); But I think there is probably a better way. Help? Edit: Could I confidently get rid of the .toString method there?

How do I determine if a string is date format?

I have some strings like this: 2012-02-05T07:42:47.000Z mixed with other strings. It is always in this format. (but the numbers are not the same, of course...the times are different) (not Sun, 05 Feb 2012 07:42:47 GMT ) I want to know whether a string matches that format. How can I determine that? It's so complicated with the colons and dots and stuff.

How to spot all $tickers in string?

Consider the following string: $MRK - Merck - ($AAPL, $MSFT, $F) having day $AA! like $P and me :) Although it's jibberish, it shows my problem. I want to scan for all words starting with a dollar-sign ($) and check them against a pre-defined list of tickers. If there's a match, replace them with a label ({TICKER}), as follows: {TICKER} - Merck - ({TICKER}, {TICKER},{TICKER}) having a day {TICKER}! like {TICKER} an me :) I now use this function: function _process_tickers($string) { $result = db_query("SELECT symbol FROM us_stocks"); while ($row = db_fetch_object($result)) { $tickers[] = ' $' . $row->symbol . ' '; } return str_replace($tickers, ' {TICKER} ', $tweet); } Problem: this only catches tickers that are surrounded by spaces (this $AA is surrounded by spaces) but not other situations like (this ticker has only a space in in front $AA) or (this one is surrounded by commas: my,$AA, ticker). But also two tickers right

Exclude a certain number from this regex

I have this regex that test for an input with max length of 5. My problem is I want to exclude the single digit of 2. If string contains only number "2", it should fail. How do I exclude number 2 in this regex? /^([a-zA-Z,\d]){1,5}$/ 13425 - Match 03277 - Match 2 - Fail.

Needing to expand this regex URL/mail parser a bit further

function make_clickable($text) { $ret = ' ' . $text; $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", "'\\1<a href=\"\\2\" >\\2</a>'", $ret); $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1<a target=\"_blank\" href=\"http://\\2\" >\\2</a>'", $ret); $ret = preg_replace("#(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret); $ret = substr($ret, 1); return($ret); } Problems: http://www.google.com , http://www.google.com ; http://www.google.com . http://www.google.com : http://www.google.com ? (http://www.google.com) as well as make sure that other domains lik

jquery match element based on id

I'm a noob to jquery. Stuck and would appreciate some help. Currently building a tool to grab data from a particular page. The page looks like this: <table width="100%" border="0" align="center" cellpadding="3" cellspacing="1"> <tbody> //This is data group 1 <tr id="parent0"> //Record 1 <td align="left"> <---------- text1 here -------> </td> <td align="left"> <---------- text2 here -------> </td> <td align="left"> <---------- text3 here -------> </td> </tr> <tr id="parent0"> //Record 2 <td align="left"> <---------- text1 here -------> </td> <td align="left"> <---------- text2 here -------> </td> <td align="left"> <---------- text3 here -------> </td> </tr> </tb