Forum Moderators: coopster

Message Too Old, No Replies

Solution possible by reg exp.?

         

Claes100

10:16 am on Nov 9, 2011 (gmt 0)

10+ Year Member



Hi all!
I am pretty bad in regular exoressions but think my problem can be solved with it...

Basically, I have a feature that will copy all db rows (type=text) for one user to another and thus create new (auto-incremented pageId:s) db rows. In some fields there are URL references to other rows (unique pageId) and that's where my problem is.
I need to perform the following:

  1. Loop through each row for user A
  2. For each row, copy the row in the dB to user B and store original_pageId and created_pageId in an array
  3. After all rows are copied: Loop through each row for user B and find all occurences of string "/users/index.php?pageId="
  4. In php, grab whatever comes after the 'pageId=' (can be 3, 99, 862... any integer)
  5. Find the pageId in the array to retrieve the new, created_pageId
  6. Replace in the db e.g. "/users/index.php?pageId=243" with "/users/index.php?pageId=1442"


Did I make it clear?
If so, is this a good approach or can I somehow use regexp to get "whatever comes after the 'pageId'"?

Thanks!
/Claes

lucy24

10:53 pm on Nov 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Piece of cake ;)

The exact wording and syntax will depend on your program's RegEx dialect, so try it on a mockup before letting it loose on the real thing.

You can do it with a capture:

(/users/index\.php\?pageId=)original_pageId
change to
$1created_pageId

(or possibly \1created_pageId depending on your dialect).

Or with a lookbehind:

(?<=/users/index\.php\?pageId=)original_pageId
change to
created_pageId

All RegEx dialects require you to \ escape literal periods and question marks so they become \. and \? In php, I kinda think you also have to \ escape literal slashes so they become \/

"Literal period" etc means "the actual period character, as distinct from the RegEx . which has meaning by itself".

That was assuming "original_pageID" and "created_pageID" are named variables that you can get at. If you need to use the actual numbers, it becomes

(/users/index\.php\?pageId=){number1}

changing into

$1{number2}

using the actual numbers.

Claes100

7:46 am on Nov 10, 2011 (gmt 0)

10+ Year Member



Thanks lucy24,
I'll give it a try!