Jump to content

ddrudik

Members
  • Posts

    78
  • Joined

  • Last visited

Everything posted by ddrudik

  1. Show what the resulting array should look like.
  2. The vowel code seems simple enough, although to demonstrate how you might do this with regex functions alone: <?php $sourcestring="Dang this crap, I want to SHOOT somebody. But that would be dangerous!"; echo preg_replace('/(?<=\bCR)A(?=P\b)|(?<=\bSH)OO(?=T\b)/ie','preg_replace(\'/./\',\'*\',\'\0\')',$sourcestring); ?>
  3. Must not be the same then. Consider providing the URL for testing.
  4. <?php $sourcestring="your source string"; preg_match('~<table style="border-collapse: collapse;" id="table9" bordercolorlight="#FFFFFF" border="1" width="100%">(.*?)</table>~s',$sourcestring,$matches); echo "<pre>".print_r($matches,true); ?>
  5. Your code works for me, maybe your real-world source is different than your sample: <pre> <?php $content='class="l">85.236.100.103 <img src="/i/launch.gif" title="Launch"/></a></td><td>29060 class="l">85.236.100.103 <img src="/i/launch.gif" title="Launch"/></a></td><td>29060 class="l">85.236.100.103 <img src="/i/launch.gif" title="Launch"/></a></td><td>29060'; preg_match_all('|class="l">([0-9.]+) <img src="/i/launch.gif" title="Launch"/></a></td><td>([0-9]+)|', $content, $matches); echo htmlentities(print_r($matches,true)); ?> output: Array ( [0] => Array ( [0] => class="l">85.236.100.103 <img src="/i/launch.gif" title="Launch"/></a></td><td>29060 [1] => class="l">85.236.100.103 <img src="/i/launch.gif" title="Launch"/></a></td><td>29060 [2] => class="l">85.236.100.103 <img src="/i/launch.gif" title="Launch"/></a></td><td>29060 ) [1] => Array ( [0] => 85.236.100.103 [1] => 85.236.100.103 [2] => 85.236.100.103 ) [2] => Array ( [0] => 29060 [1] => 29060 [2] => 29060 ) )
  6. Please show what matches you want from that string.
  7. You could also incorporate the height and width params into the regex, but with only your initial requirement: $html=preg_replace('~<object\s[^>]*/swflash.cab[^>]*><param\s[^>]*value=("[^"]*")(??!</object>).)*</object>~is','<a href=$1 style="display:block;width:400px;height:300px" id="player"></a>',$html);
  8. $html=preg_replace('#<a\s[^>]*\bhref="(?=(??!/|https?://)[^"])+)#i','$0/',$html); haystack: <a href="blah">bleh</a> With <a href="/blah">bleh</a> <a href="blah/">bleh</a> With <a href="/blah">bleh</a> <a href="http://blah">bleh</a> With <a href="https://blah">bleh</a> output: <a href="/blah">bleh</a> With <a href="/blah">bleh</a> <a href="/blah/">bleh</a> With <a href="/blah">bleh</a> <a href="http://blah">bleh</a> With <a href="https://blah">bleh</a>
  9. As for how to handle the other tags, consider similar questions here to handle those as well as what to do with mismatched tags etc.: http://regexadvice.com/forums/thread/45583.aspx http://regexadvice.com/forums/thread/46297.aspx
  10. Usually CMS templating systems such as this evolve into something more complex than the example, do you have plans to support [img...] [url...] tags etc.? For your original question: $str=preg_replace('/^[^\r\n]+/m','<p>$0</p>',$str);
  11. I was reading the 'Preview' as being the same as the actual post afterwards. The preview window shows them removed. The string below in regex notation appears as: ^\t\t$result['level']=2;$ $result['level']=2; In the Preview it appears in a code block without indent.
  12. Tab character seem to fail to indent when displayed within [ code ] blocks here but if I post the code without a [ code ] block the code displays indented without issue. The color markup of the code is all well and good but if it is going to mess with the indent I don't see the benefit. Is there something I am missing? example: function parseline($line){ if(substr($line,178,1)=='2'){ function parseline($line){ if(substr($line,178,1)=='2'){
  13. This is actually a string question and not a regex question. Here's the code required to parse the lines into fields within the files, I will leave it to you to work out the specifics on how you want to compare what. The code and array output of the example (shown with file1 but file2 is parsed with the same code) should give you a start in the right direction. <pre> <?php function parseline($line){ if(substr($line,178,1)=='2'){ $result['level']=2; $result['indent']=substr($line,0,1); $result['address']=substr($line,1,78); $result['city']=substr($line,79,30); $result['state']=substr($line,110,2); $result['zip']=substr($line,113,5); $result['tollfree']=substr($line,123,40); $result['areacode']=substr($line,164,3); $result['prefix']=substr($line,168,3); $result['suffix']=substr($line,172,4); } elseif(substr($line,178,1)=='1') { $result['level']=1; $result['page']=substr($line,0,4); $result['type']=substr($line,5,1); $result['name']=substr($line,7,78); } else { return false; } return array_map('trim',$result); } $lines=file('file1.txt'); foreach($lines as $line){ $fields=parseline($line); if($fields){ echo "<hr>line:<br>$line<br>\$fields "; echo print_r($fields,true); } } ?>
  14. To do that level of detail comparison each line would need to be broken out into fields by a different method than what I used. Your file1 uses \r\n as line separators while file2 only uses \n so that was throwing off my code. My code compares both lines of a record together so it's output is different than from your program. <pre> <?php function showdiff($f1,$f2){ $file1=preg_replace('/ +/',' ',preg_replace('/(.*?)\S+(?=\r\n)/','$1',file_get_contents('file1.txt'))); $file2=preg_replace('/ +/',' ',preg_replace('/(.*?)\S+(?=\r\n)/','$1',preg_replace('/\n/',"\r\n",file_get_contents('file2.txt')))); $f1count=preg_match_all('/(?:.*?\r\n){2}/',$file1,$f1matches); $f2count=preg_match_all('/(?:.*?\r\n){2}/',$file2,$f2matches); foreach($f2matches[0] as $f2line){ if(!in_array($f2line,$f1matches[0])){ echo "missing <font color=red>$f2line</font> from file 1.<br>"; } } foreach($f1matches[0] as $f1line){ if(!in_array($f1line,$f2matches[0])){ echo "missing <font color=red>$f1line</font> from file 2.<br>"; } } } showdiff('file1','file2'); ?>
  15. Please show what specific output you expect when comparing the two file samples as shown in your original question.
  16. They were compared between the two files as two-line records (name etc on line 1 and address etc on line 2). preg_match_all put them into into an array per file and then I compared them between the two arrays.
  17. See if this works for your requirements: <pre> <?php function showdiff($f1,$f2){ $file1=preg_replace('/ +/',' ',preg_replace('/(.*?)\S+(?=\r\n|$)/','$1',file_get_contents('file1.txt'))); $file2=preg_replace('/ +/',' ',preg_replace('/(.*?)\S+(?=\r\n|$)/','$1',file_get_contents('file2.txt'))); preg_match_all('/.*?\r\n.*?(?:\r\n|$)/',$file1,$f1matches); preg_match_all('/.*?\r\n.*?(?:\r\n|$)/',$file2,$f2matches); foreach($f2matches[0] as $f2line){ if(!in_array($f2line,$f1matches[0])){ echo "missing <font color=red>$f2line</font> from file 1.<br>"; } } foreach($f1matches[0] as $f1line){ if(!in_array($f1line,$f2matches[0])){ echo "missing <font color=red>$f1line</font> from file 2.<br>"; } } } showdiff('file1.txt','file2.txt'); ?> output: missing 0003 R ALSTON BRANDY N 310 OBERLIN RD 326-0369 from file 1. The spacing is different between the two files so multiple spaces have been reduced to 1 space and the last column which also differs between the two files is ignored. The comparison is irrespective of location in the file, it is a success if a given 2-line record in file1 is located anywhere in file2 and vice versa. Your last entry in both files was excluded from my testing since they included only line 1 and not line 2, making a comparison of both not possible.
  18. A more common way of seeing that is with (?R) instead of (?0) although (?0) helps to illustrate that you could incorporate lookahead and lookbehind and use a capture group 1 as the nested pattern (?1). A complete background is in Friedl's "Mastering Regular Expressions" but for a quick PHP regex syntax overview: http://us3.php.net/manual/en/reference.pcre.pattern.syntax.php Search for "Recursive Patterns" on that page and you will see the discussion of the general pattern, although instead their example matches nested/non-nested parens groups. It is simpler to construct a pattern with a single bounding character such as ( ) versus the table tags but the theory is the same.
  19. The text files differ quite a bit, 1/2 vs. V1/V2 in the last column etc, is the last column to be ignored from the comparison? Are your real-world text files to be compared line by line exactly or every 2 lines as the records appear to be 2 lines long? Is the order important or can the records appear anywhere in the text file to be considered valid? What about records in file1 that don't appear in file2 (if that should ever occur)?
  20. Is it sufficient to match all {{...}} blocks? If so: <?php $sourcestring="{{param1:123_xyz^param2:xyx^param3:123}}"; preg_match_all('/\{\{.*?\}\}/s',$sourcestring,$matches); echo "<pre>".print_r($matches,true); ?> $matches Array: ( [0] => Array ( [0] => {{param1:123_xyz^param2:xyx^param3:123}} ) )
  21. Probably should bound that with ^ and $ to test entire string: <?php $str = "15-03-1991"; echo preg_match('/^\d{2}-\d{2}-\d{4}$/', $str) ? "Good" : "Bad" ; ?>
  22. That last code brings up a good point, for every regex function there's a string function that can do the same operation faster and with less overhead.
  23. <?php $html='<h1>Some tables and text</h1> <table> <tr> <th>England</th> <th>Paris</th> <th>Munich</th> </tr> <tr> <td> <table> <tr> <th>London</th> <th>Brighton</th> <th>Cambridge</th> </tr> <tr> <td>rain</td> <td>sun</td> <td>wind</td> </tr> </table> </td> <td>sun</td> <td>wind</td> </tr> </table> This is a text between the tables wich should not be removed. <table> <tr> <th>London</th> <th>Paris</th> <th>Munich</th> </tr> <tr> <td>rain</td> <td>sun</td> <td>wind</td> </tr> </table>'; $html=preg_replace('~<table[^>]*>(??>(??!</?table[^>]*>).)+)|(?0))*</table>~is','',$html); echo $html; ?>
  24. Those were just various code examples for the task at hand. For your last question: $newStr = preg_replace('#<img src=("[^"]*") [^>]+>#', '<a href=$1>$0</a>', $str);
  25. If the can be any non-tag text: preg_match_all('~<p class="pagination" align=center>[^<]*<a [^>]*>([^<]*)</a>[^<]*<a [^>]*>([^<]*)~',$sourcestring,$matches); If the can be any non-a href tag text (less preferred): preg_match_all('~<p class="pagination" align=center>.*?<a [^>]*>([^<]*)</a>[^<]*<a [^>]*>([^<]*)~s',$sourcestring,$matches);
×
×
  • 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.