Solved

PHP Code: Sub-Domain Site Number - Regular Expression Syntax Help Needed

Posted on 2011-03-05
7
500 Views
Last Modified: 2012-05-11
I'm working with a Wordpress Multisite Network (v3.0.4).

I need some help to parse the source code of the wp-admin page at:
    "http://www.domain.com/wp-admin/ms-sites.php".  

It needs to use "preg_match()" similar to this:

preg_match( "/name=\"_wpnonce_add-blog\" value=\"(.*?)\"/", $this -> page, $match ) ;
$value = $match[1] ;

The expression to be matched is the following:
<a href="http://www.domain.com/wp-admin/ms-sites.php?action=editblog&id=91" class="edit">subdomain91</a>

But I will use a variable such as the following:
$subDomain = 'subdomain91' ;

Then the expression will contain that variable...
<a href="http://www.domain.com/wp-admin/ms-sites.php?action=editblog&id=91" class="edit">$subDomain</a>

Ultimately, $match[1] must contain '91'

But keep in mind that for another "subdomain" name, the number will be different, id=xx.
0
Comment
Question by:WizeOwl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
7 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35042982
This will do it (below) as long as the data contains no other text with id= in it.

<?php

$data = '<a href="http://www.domain.com/wp-admin/ms-sites.php?action=editblog&#038;id=91" class="edit">$subDomain</a>';

preg_match('!.*?;id=([0-9]+)".*?!s', $data, $match );

print_r( $match

Open in new window

0
 

Author Comment

by:WizeOwl
ID: 35043112
I've attached a larger code snippet (below) that represents the source code of the wp-admin page.  (or if you have a wpms blog network, you can look at the "Super Admin > Sites" page for yourself.)
 
<td class="column-title">
<a href="http://www.rootDomain.com/wp-admin/ms-sites.php?action=editblog&#038;id=91" class="edit">subDomain91</a>

<div class="row-actions">

<span class="edit"><a href="http://www.rootDomain.com/wp-admin/ms-sites.php?action=editblog&#038;id=91">Edit</a></span> | <span class='backend'><a href='http://subDomain91.rootDomain.com/wp-admin/' class='edit'>Backend</a></span> | <span class="activate"><a href="http://www.rootDomain.com/wp-admin/ms-edit.php?action=confirm&#038;action2=deactivateblog&#038;id=91&#038;msg=You+are+about+to+deactivate+the+site+subDomain91">Deactivate</a></span> | <span class="archive"><a href="http://www.rootDomain.com/wp-admin/ms-edit.php?action=confirm&#038;action2=archiveblog&#038;id=91&#038;msg=You+are+about+to+archive+the+site+subDomain91.">Archive</a></span> | <span class="spam"><a href="http://www.rootDomain.com/wp-admin/ms-edit.php?action=confirm&#038;action2=spamblog&#038;id=91&#038;msg=You+are+about+to+mark+the+site+subDomain91+as+spam.">Spam</a></span> | <span class="delete"><a href="http://www.rootDomain.com/wp-admin/ms-edit.php?action=confirm&#038;action2=deleteblog&#038;id=91&#038;msg=You+are+about+to+delete+the+site+subDomain91.">Delete</a></span> | <span class='view'><a href='http://subDomain91.rootDomain.com' rel='permalink'>Visit</a></span>	</div>
</td>

Open in new window



Here is my best shot at the preg_match code. Perhaps you can help me clean it up?


$rootDomain = rootdomain ;
$subDomain = subdomain91 ;

preg_match( "/<a href=\"http:\/\/www\." . $rootDomain . "\/wp-admin/ms-sites\.php\?action=editblog&#038;id=(.*?)\" class=\"edit\">" . $subDomain . "<\/a>/", $this -> page, $matchID ) ;
return "Match ID: " . $matchID[1] ;

However, there's a syntax "Warning" when I run the code...
<< Warning: preg_match(): Unknown modifier '-' in F:\WAMP Server\www\wpModules.php on line 94 >>

I don't know why it doesn't like the minus symbol.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35043548
You forgot to escape one of your slashes here:

    "\/wp-admin/ms-sites\.

PHP is picking up the "/ms" as pattern modifiers (multi-line and single-line, respectively), and then trying to interpret the dash as a modifier as well (and the rest of the line if the dash were actually a modifier).
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

Author Comment

by:WizeOwl
ID: 35045032
It is very close then.

I need to capture the string here, (.*?), which I thought would be in the $matchID[] array. But when I echo $matchID[0], $matchID[1], and $matchID[2], there seems to be nothing there.

Can you suggest how to accomplish this?
0
 

Author Comment

by:WizeOwl
ID: 35045101
This (below) is probably more accurate, now that I examined the syntax of preg_match() more carefully. However, it is still not working. Neither print_r shows any content from the $match array.

// Capture the first match
preg_match( "/<a href=\"http:\/\/www\." . $rootDomain . "\/wp-admin\/ms-sites\.php\?action=editblog&#038;id=(.*?)\" class=\"edit\">" . $subDomain . "<\/a>/", $this -> page, $matchALL ) ;
print_r( $matchALL[1] ) ;

// Now, isolate the important part
preg_match( "/id=(.*?)/", $matchALL[1], $matchID ) ;
print_r( $matchID[1] ) ;
0
 

Assisted Solution

by:WizeOwl
WizeOwl earned 0 total points
ID: 35045859
SOLUTION FOUND:

// First, navigate to the 'sites' page
//
$this -> newLocation( $this -> web_url . "/wp-admin/ms-sites.php" ) ;

// Then, look for the correct pattern to match.
// You want to find the ID that corresponds to the subdomain being processed
// Note: '@' symbol is the delimiter and replaces '/'
//
preg_match( "@www\." . $rootDomain . "/wp-admin/ms-sites\.php\?action=editblog&#038;id=(.*?)\" class=\"edit\">" . $subDomain . "@", $this -> page, $matchALL ) ;

// Print the results to confirm you have a correct match.
// Use print_r() for arrays, and print() for simple strings.
//
print( "matchALL: " ) ;
print_r( $matchALL ) ;
print( "<br />" ) ;

$ID = explode( "id=", $matchALL[1] ) ;
print( "ID (explode): " ) ;
print_r( $ID ) ;
print( "<br />" ) ;
print( "ID: " ) ;
print_r( $ID[0] ) ;
print( "<br />" ) ;
0
 

Author Closing Comment

by:WizeOwl
ID: 35107029
Thanks, kaufmed, for your assistance.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question