Search Results

Search found 6715 results on 269 pages for 'preg match'.

Page 1/269 | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • Is there a way to pass another parameter in the preg_replace_callback callback function?

    - by DaNieL
    mmmh guys, i really hope my english is good enaught to explain what i need. Lets take this example (that is just an example!) of code: class Something(){ public function Lower($string){ return strtolower($string); } } class Foo{ public $something; public $reg; public $string; public function __construct($reg, $string, $something){ $this->something = $something; $this->reg = $reg; $this->string = $string; } public function Replace(){ return preg_replace_callback($this->reg, 'Foo::Bar', $this->string); } public static function Bar($matches){ /* * [...] * do something with $matches and create the $output variable * [...] */ /* * I know is really useless in this example, but i need to have an istance to an object here * (in this example, the Something object, but can be something else!) */ return $this->something->Lower($output); } } $s = new Something(); $foo = new Foo($myregexp, $mystring, $s); $content = $foo->Replace(); So, the php manual say that to use a class method as callback in preg_replace_callback(), the method must be abstract. I need to pass an instance of a previuosly initialized object (in the example, an instance of the Something class) at the callback function. I tryed to use call_user_func(), but doesnt work (becose in this way i miss the matches parameter). Is there a way to do that, or have i to separate the process (doing before preg_match_all, for each match retrieve the replace value, and then a simple preg_replace)?

    Read the article

  • How do I get preg replace to work when the url contains a hashtag

    - by Steven
    I found a function online for turning a url within a string into a clickable link. However, when the url contains a hashtag it doesn't work. eg. http://www.bbc.co.uk/radio1/photos/fearnecotton/5759/1#gallery5759 Here's the part of the function concerned: $ret = preg_replace( "#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret ); $ret = preg_replace( "#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret ); Any ideas? thanks

    Read the article

  • preg_match to match an optional string, but not match all of the string

    - by buggedcom
    Take for example the following regex match. preg_match('!^publisher/([A-Za-z0-9\-\_]+)/([0-9]+)/([0-9]{4})-(january|february|march|april|may|june|july|august|september|october|november|december):([0-9]{1,2})-([0-9]{1,2})/([A-Za-z0-9\-\_]+)/([0-9]+)(/page-[0-9]+)?$!', 'publisher/news/1/2010-march:03-23/test_title/1/page-1', $matches); print_r($matches); It produces the following: Array ( [0] => publisher/news/1/2010-march:03-23/test_title/1/page-1 [1] => news [2] => 1 [3] => 2010 [4] => march [5] => 03 [6] => 23 [7] => test_title [8] => 1 [9] => /page-1 ) However as the last match is optional it can also work with matching the following "publisher/news/1/2010-march:03-23/test_title/1". My problem is that I want to be able to match (/page-[0-9]+) if it exists, but match only the page number so "publisher/news/1/2010-march:03-23/test_title/1/page-1" would match like so: Array ( [0] => publisher/news/1/2010-march:03-23/test_title/1/page-1 [1] => news [2] => 1 [3] => 2010 [4] => march [5] => 03 [6] => 23 [7] => test_title [8] => 1 [9] => 1 ) I've tried the following regex '!^publisher/([A-Za-z0-9\-\_]+)/([0-9]+)/([0-9]{4})-(january|february|march|april|may|june|july|august|september|october|november|december):([0-9]{1,2})-([0-9]{1,2})/([A-Za-z0-9\-\_]+)/([0-9]+)/?p?a?g?e?-?([0-9]+)?$!' This works, however it will also match "publisher/news/1/2010-march:03-23/test_title/1/1". I have no idea to perform a match but not have it come back in the matches? Is it possible in a single regex?

    Read the article

  • JS/Jquery, Match not finding the PNG = match('/gif|jpg|jpeg|png/')

    - by nobosh
    I have the following code which I use to match fancybox possible elements: $('a.grouped_elements').each(function(){ var elem = $(this); // Convert everything to lower case to match smart if(elem.attr('href').toLowerCase().match('/gif|jpg|jpeg|png/') != null) { elem.fancybox(); } }); It works great with JPGs but it isn't matching PNGs for some reason. Anyone see a bug with the code? Thanks

    Read the article

  • php preg replace

    - by David
    I'm trying to build a PHP preg replace string when processing poorly written xml, such that if I am given: $x='<abc x="y"><def x="g">more test</def x="g"><blah>test data</blah></abc x="y">'; That it checks if there's a space within a closing tag and deletes everything from the space to the end of the tag such that. becomes <abc x="y"><def x="g">more test</def><blah>test data</blah></abc> thanks

    Read the article

  • Regex: match a non nested code block

    - by Sylvanas Garde
    I am currently writing a small texteditor. With this texteditor users are able to create small scripts for a very simple scripting engine. For a better overview I want to highlight codeblocks with the same command like GoTo(x,y) or Draw(x,y). To achieve this I want to use Regular Expresions (I am already using it to highlight other things like variables) Here is my Expression (I know it's very ugly): /(?<!GoTo|Draw|Example)(^(?:GoTo|Draw|Example)\(.+\)*?$)+(?!GoTo|Draw|Example)/gm It matches the following: lala GoTo(5656) -> MATCH 1 sdsd GoTo(sdsd) --comment -> MATCH 2 GoTo(23329); -> MATCH 3 Test() GoTo(12) -> MATCH 4 LALA Draw(23) -> MATCH 5 Draw(24) -> MATCH 6 Draw(25) -> MATCH 7 But what I want to achieve is, that the complete "blocks" of the same command are matched. In this case Match 2 & 4 and Match 5 & 6 & 7 should be one match. Tested with http://regex101.com/, the programming lanuage is vb.net. Any advise would be very useful, Thanks in advance!

    Read the article

  • A maximum character limit on the preg functions?

    - by animuson
    On my site I use output buffering to grab all the output and then run it through a process function before sending it out to the browser (I don't replace anything, just break it into more manageable pieces). In this particular case, there is a massive amount of output because it is listing out a label for every country in the database (around 240 countries). The problem is that in full, my preg_match functions seems to get skipped over, it does absolutely nothing and returns no matches. However, if I remove parts of the labels (no particular part, just random pieces to reduce characters) then the preg_match functions works again. It doesn't seem to matter what I remove from the label, it just seems to be that as long as I remove so many characters. Is there some sort of cap on what the preg functions can handle or will it time out if there is too much data to be scanned over?

    Read the article

  • Preg Expression to identify classes/ids in a CSS file that have no contents

    - by dclowd9901
    I'm in the process of updating some old CSS files in our systems, and we have a bunch that have lots of empty classes simply taking up space in the file. I'd love to learn how to write Regular expressions, but I just don't get them. I'm hoping the more I expose myself to them (with a little more cohesive explanation), the more I'll end up understanding them. The Problem That said, I'm looking for an expression that will identify text followed by a '{' (some have spaces in between, and some do not) and if there are no letters or numbers between that bracket and '}' (spaces don't count), it will be identified as a matching string. I suppose I can trim the whitespace out of the doc before I run a regular expression through it, but I don't want to change the basic structure of the text. I'm hoping to return it into a large <textarea>. Bonus points for explaining the characters and their meanings, and also an expression identifying lines in the copy without any text or numbers, as well. I will likely use the final expression in PHP script. tl;dr: Regular Expression to match: .a_class_or #an_id { /* if there aren't any alphanumerics in here, this should be a matching line of text */ }

    Read the article

  • PHP: Rawurlencode inside Preg-replace

    - by elmaso
    Hello, I have this snippet while($row = mysql_fetch_array($result)){ $search = '' . $row['searchquery']; echo '<a href="http://www.example.com/' . preg_replace( array('/[^\s\w]/','/\s/'),array('','+'),$search) . '+/">' ... but if someone types in äöü, it doesnt show the letters - with rawurlencode thats possible, but I want to remove the blank spaces with pregreplace and replace it with + is that possible?

    Read the article

  • in php how do I use preg replace to turn a url into a tinyurl

    - by Steven
    I need to convert a string of text containing a long url into the same string but with a tinyurl (using the tinyurl api). eg. convert "blah blah blah /http://example.com/news/sport blah blah blah" into "blah blah blah http://tinyurl.com/yaeocnv blah blah blah". How can it be done? PLEASE NOTE I added a slash before the long url as I'm only allowed to post one link

    Read the article

  • preg replace h2 tags with spaces [closed]

    - by fire
    Possible Duplicate: PHP Regular express to remove <h1> tags (and their content) I have some HTML that looks like this: <h2> Fund Management</h2> <p> The majority of property investments are now made via our Funds.</p> Trying to use a regular expression to strip h2 tags but doesn't work because of the space between the opening and closing h2 tags. preg_replace('/<h2>(.+?)<\/h2>/', '', $content); Any ideas on how to make this work? Also I would ideally like it to replace h1-h6 tags so maybe it needs [1-6] or something?

    Read the article

  • Use regular expressions to match an ? but not a \?

    - by J.Milly
    I have a PHP regular expression that has been functioning fairly well to parse some odd legacy client templates until recently when we found an escaped question mark (\?) included in a template expression. I'm not strong enough with my regular expression-fu to wrap my feeble noodle around a negative look ahead or some techno-mumbo-jumbo so, tips or points in the right direction would be greatly appreciated. My PHP: preg_match_all("/\{\{IF (.*)\?(.*):(.*)\}\}/U", $template, $m, PREG_SET_ORDER);

    Read the article

  • Regular expression to match one of two video ID's in a Google Video URL

    - by Baldur
    I need to grab the video ID from a Google Video URL. There are two different types of URLs that I need to be able to match: http://video.google.com/videoplay?docid=-3498228245415745977# where I need to be able to match -3498228245415745977 (note the dash; -), and video.google.com/videoplay?docid=-3498228245415745977#docid=2728972720932273543 where I need to match 2728972720932273543. Is there any good regular expression that can match this? This is what I've got so far: @"docid=(-?\d{19}+)" since the video ID seems to be 19 characters except when it's prefixed with the dash. I'm using C# (of which I have very little experience) if that changes anything. P.s. I would also appreciate you review my regular expressions for YouTube (@"[\?&]v=([^&#])";), RedTube (@"/(\d{1,6})") and Vimeo (@"/(\d*)"). I do not expect users to enter the full URL and thus do not match the ^http://\\.?sitename+\\.\\w{2,3}.

    Read the article

  • mysql match against hebrew

    - by Devenv
    Hey, Trying to solve this for a very long time now... SELECT MATCH(name) AGAINST('??????') (hebrew) doesn't work, but SELECT MATCH(name) AGAINST('abraxas') SELECT MATCH(name) AGAINST('????????') (english, russian) work perfectly. I know it's something with character-set, but I tried all kind of settings and it didn't work. For now it's latin-1. LIKE works I'm pretty much sure that any weird language like arabic etc won't work...

    Read the article

  • Python File Search Line And Return Specific Number of Lines after Match

    - by Simos Anderson
    I have a text file that has lines representing some data sets. The file itself is fairly long but it contains certain sections of the following format: Series_Name INFO Number of teams : n1 | Team | # | wins | | TeamName1 | x | y | . . . | TeamNamen1 | numn | numn | Some Irrelevant lines Series_Name2 INFO Number of teams : n1 | Team | # | wins | | TeamName1 | num1 | num2 | . where each section has a header that begins with the Series_Name. Each Series_Name is different. The line with the header also includes the number of teams in that series, n1. Following the header line is a set of lines that represents a table of data. For each series there are n1+1 rows in the table, where each row shows an individual team name and associated stats. I have been trying to implement a function that will allow the user to search for a Team name and then print out the line in the table associated with that team. However, certain team names show up under multiple series. To resolve this, I am currently trying to write my code so that the user can search for the header line with series name first and then print out just the following n1+1 lines that represent the data associated with the series. Here's what I have come up with so far: import re print fname = raw_input("Enter filename: ") seriesname = raw_input("Enter series: ") def findcounter(fname, seriesname): logfile = open(fname, "r") pat = 'INFO Number of teams :' for line in logfile: if seriesname in line: if pat in line: s=line pattern = re.compile(r"""(?P<name>.*?) #starting name \s*INFO #whitespace and success \s*Number\s*of\s*teams #whitespace and strings \s*\:\s*(?P<n1>.*)""",re.VERBOSE) match = pattern.match(s) name = match.group("name") n1 = int(match.group("n1")) print name + " has " + str(n1) + " teams" lcount = 0 for line in logfile: if line.startswith(name): if pat in line: while lcount <= n1: s.append(line) lcount += 1 return result The first part of my code works; it matches the header line that the person searches for, parses the line, and then prints out how many teams are in that series. Since the header line basically tells me how many lines are in the table, I thought that I could use that information to construct a loop that would continue printing each line until a set counter reached n1. But I've tried running it, and I realize that the way I've set it up so far isn't correct. So here's my question: How do you return a number of lines after a matched line when given the number of desired lines that follow the match? I'm new to programming, and I apologize if this question seems silly. I have been working on this quite diligently with no luck and would appreciate any help.

    Read the article

  • javascript regex: match altered version of first match with only one expression

    - by theseion
    Hi there I'm writing a brush for Alex Gorbatchev's Syntax Highlighter to get highlighting for Smalltalk code. Now, consider the following Smalltalk code: aCollection do: [ :each | each shout ] I want to find the block argument ":each" and then match "each" every time it occurrs afterwards (for simplicity, let's say every occurrence an not just inside the brackets). Note that the argument can have any name, e.g. ":myArg". My attempt to match ":each": \:([\d\w]+) This seems to work. The problem is for me to match the occurrences of "each". I thought something like this could work: \:([\d\w]+)|\1 but the right hand side of the alternation seems to be treated as an independent expression, so backreferencing doesn't work. So my question is: is it even possible to accomplish what I want in a single expression? Or would I have to use the backreference within a second expression (via another function call)? Cheers.

    Read the article

  • Add links to specific words within span tag in PHP

    - by dazhall
    I have a list of words that I'd like to add a link to, I can do this fairly easily using preg_match_all and preg_replace: $str = "<span class=\"cz\">Dám si jedno pivo prosím.</span> = I'll have a beer please."; preg_match_all('/[a-ztúuýžácdéeínórš]+/i',$str,$matches); $matches = array_unique($matches[0]); foreach ($matches as $match) { if(!empty($words[$match])) { $str = preg_replace("/(^|[^\w]){1}(".preg_quote($match,"/").")($|[^\w]){1}/i", '\\1<a href="#">\\2</a>\\3', $str); } } echo $str; What I'd like to do is restrict the linking to only within the span tag. My brain is all regex-ed out, so any help would be appreciated! Thanks! Darren.

    Read the article

  • Google Contacts Error: If-Match or If-None-Match header or entry etag attribute required

    - by Ali
    Hi guys I'm following the example code as defined on this website: http://www.ibm.com/developerworks/opensource/library/x-phpgooglecontact/index.html I want to be able to integrate with my google apps account and play around with teh contacts. However I get this error whenever I try to run the modify contacts code: If-Match or If-None-Match header or entry etag attribute required This is my code: Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); Zend_Loader::loadClass('Zend_Http_Client'); Zend_Loader::loadClass('Zend_Gdata_Query'); Zend_Loader::loadClass('Zend_Gdata_Feed'); $client = getGoogleClient('cp'); // this is a function I made - its working fine $client->setHeaders('If-Match: *'); $gdata = new Zend_Gdata($client); $gdata->setMajorProtocolVersion(3); $query = new Zend_Gdata_Query($id);// id is the google reference $entry = $gdata->getEntry($query); $xml = simplexml_load_string($entry->getXML()); $xml->name->fullName = trim($contact->first_name).' '.trim($contact->last_name); $entryResult = $gdata->updateEntry($xml->saveXML(), $id);

    Read the article

  • Regex negative match query

    - by Matt
    Hey guys, I've got a regex issue, Im trying to ignore just the number '41', I want 4, 1, 14 etc to all match. I've got this [^\b41\b] which is effectively what I want but this also ignores all single iterations of the values 1 and 4. As an example, this matches "41", but I want it to NOT match: \b41\b

    Read the article

  • Regex string match?

    - by Andy
    I have a long string in javascript like var string = 'abc234832748374asdf7943278934haskhjdasfhjkdfas83421def8923487234897234897'; I am trying to match like abc234832748374 and def8923487234897 - that is - I have tried like string.match(\abc[^abc]|\def[^def]|) but that doesnt get me both strings because I need numbers after them ? Basically I need abc + 8 chars after and def the 8-11 chars after ? How can I do this ?

    Read the article

  • PHP preg_replace - Don't match with h1 tags

    - by James
    Hi there. I am using preg_replace to add a link to keywords if they are found within a long HTML string. I don't want to add a link if the keyword is found within h1 tags or strong tags. The below regex nearly works and basically says (I think): If the keyword is not immediately wrapped by either a h1 tag or a strong tag then replace with the keyword that was matched, as a bolded link to google. $result = preg_replace('%(?!<h1>)(?!<strong>)\b(bobs widgets)\b(?!<\/strong>)(?!<\/h1>)%i','<a href="http://www.google.com"><strong>$1</strong></a>', $result, -1); (the reason I don't want to match if in strong tags is because I am recursing through a lot of keywords so don't want to link an already linked keyword on subsequent passes) the above works fine and won't match: <h1>bobs widgets</h1> It will however match the keyword in the following text, because the h1 tag isn't immediately either side of the keyword: <h1>Here are bobs widgets for sale</h1> I need to make the spaces either side optional and have tried adding \s* but that doesn't get me anywhere. I'd be very grateful for a push in the right direction here.

    Read the article

  • PHP preg_replace - Don't match within h1 tags

    - by James
    Hi there. I am using preg_replace to add a link to keywords if they are found within a long HTML string. I don't want to add a link if the keyword is found within h1 tags or strong tags. The below regex nearly works and basically says (I think): If the keyword is not immediately wrapped by either a h1 tag or a strong tag then replace with the keyword that was matched, as a bolded link to google. $result = preg_replace('%(?!<h1>)(?!<strong>)\b(bobs widgets)\b(?!<\/strong>)(?!<\/h1>)%i','<a href="http://www.google.com"><strong>$1</strong></a>', $result, -1); (the reason I don't want to match if in strong tags is because I am recursing through a lot of keywords so don't want to link an already linked keyword on subsequent passes) the above works fine and won't match: <h1>bobs widgets</h1> It will however match the keyword in the following text, because the h1 tag isn't immediately either side of the keyword: <h1>Here are bobs widgets for sale</h1> I need to make the spaces either side optional and have tried adding \s* but that doesn't get me anywhere. I'd be very grateful for a push in the right direction here.

    Read the article

  • Javascript .match plus jQuery keyup(), double match and strange behaviour

    - by Gremo
    Not really good in regular expression, but why when a match is found console.log fires two times? $('#name').keyup(function() { var regex = /[\€]/g; var count = (m = $(this).val().match(regex)) ? m.length : 0; // Num matches console.log(count); }); Output with 'hello': 0 0 0 0 0 After adding '€' symbol to 'hello' we have: 0 0 0 0 0 1 1 After adding 'h' symbol to 'hello€' we have: 0 0 0 0 0 1 1 1 Shouldn't be just one 1 after adding '€' to 'hello'?

    Read the article

  • Match returning a string instead of object

    - by Pablo
    This simple regex matching returns a string instead of an object on every browser but the latest firefox... text = "language. Filename: My Old School Yard.avi. File description: File size: 701.54 MB. View on Megavideo. Enter this, here:" name = text.match(/(Filename:)(.*) File /); alert(typeof(name)); as far as i know this the match function is suppose to return an object (Array). Has anyone come across this issue?

    Read the article

1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >