Php preg_replace usage

Source: Internet
Author: User
Php preg_replace uses the following existing xml file:
...
 
  
  
   35
  
  $35.00K
  index.php?module=Opportunities&action=index&query=true&searchFormTab=advanced_search&lead_source=
  
   
    Prospecting
    
     0
    
    $0.00K
    index.php?module=Opportunities&action=index&query=true&searchFormTab=advanced_search&lead_source=&sales_stage=Prospecting
   
   
    Qualification
    
     0
    
    $0.00K
    index.php?module=Opportunities&action=index&query=true&searchFormTab=advanced_search&lead_source=&sales_stage=Qualification
   ....
  
 

There is a way to process xml, replace the content of the node link, and encode the content of the link with urlencode:
Function processXML ($ xmlFile) {if (! File_exists ($ xmlFile) {$ GLOBALS ['log']-> debug ("Cannot open file ($ xmlFile)") ;}$ pattern = array (); $ replacement = array (); $ content = file_get_contents ($ xmlFile); $ content = $ GLOBALS ['locale']-> translateCharset ($ content, 'utf-16le ', 'utf-8'); // This line has a problem $ pattern [] = '/\
 
  
([A-zA-Z0-9 #? & %.; \ [\] \/= + _-\ S] +) \ <\/link \>/e '; $ replacement [] = "'
  '. Urlencode (\ "$1 \").''"; Return preg_replace ($ pattern, $ replacement, $ content );}
 

The regular expression in the code above can be used in php5.4, but the e parameter is canceled in versions later than 5.5. I tried to rewrite with preg_replace_callback, but failed. the code of preg_replace_callback is as follows:
$content = preg_replace_callback('|
 ([a-zA-Z0-9#?&%.;\[\]\/=+_-\s]+)<\/link>|',function ($matches) {$u = urlencode($matches[1]);return "
 ".$u."";},$content);

Running has the following error: Warning: preg_replace_callback (): Compilation failed: invalid range in character class at offset 34

How can I modify it? the system environment is php5.6.21.


Reply to discussion (solution)

$content = preg_replace_callback(            '/\
 
  (.+?)\<\/link\>/',            function ($matches) {                return "
  ".urlencode($matches[1])."";            },            $content        );
 

$content = preg_replace_callback(            '/\
 
  (.+?)\<\/link\>/',            function ($matches) {                return "
  ".urlencode($matches[1])."";            },            $content        );
 

I tried the same regular expression instead of matching the regular expression. I tried the regular expression on the online regular expression test page, but the php method won't work.

I tested your code and didn't find anything wrong.
Maybe you have posted an error.

I tested your code and didn't find anything wrong.
Maybe you have posted an error.


Your php version and content are correct. it has never been successful.

Php5.4.31 and php5.6.13. the test is complete.
The code you posted during my testing does not rule out that you have been eaten by CSDN for illegal characters before pasting.

But yours
'| ([A-zA-Z0-9 #? & %.; \ [\] \/= + _-\ S] +) <\/link> |'
It is indeed wrong!
-Represents the range in square brackets. if it is a-character, it should be written in

Compilation failed: invalid range in character class
\ S indicates space, tab, carriage return, and line feed.
What kind of character range should _-\ s represent?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.