Hi,
i need help with preg_match
i hawe links like
http://test.site.com/cat1/cat2/123.html
http://test.site.com/cat1/cat2/cat3/1.html
i need when i put on form this link and pres submit to get link to last / aded text page- get the numberof html page and change the .html in a .txt like:
http://test.site.com/cat1/cat2/page-123.txt
http://test.site.com/cat1/cat2/cat3/page-1.txt
Thanks
Sorry for bad english!!!

Member Avatar for langsor

Hi

Regular expressions are tricky and can be very picky. If you have different input structures than the ones you provided, I cannot guarantee it to work with those other structures.

Hope it helps

<?php
// TEST SUBJECT STRINGS
$link1 = 'http://test.site.com/cat1/cat2/123.html';
$link2 = 'http://test.site.com/cat1/cat2/cat3/1.html';

// find this pattern
$pattern = '#^(.*/)(\d+)(\.html)$#';
// make these replacements
$replace = '\\1page-\\2.txt';

// regular expression preg_replace ...
print preg_replace( $pattern, $replace, $link1 );
print "\n<br>\n";
print preg_replace( $pattern, $replace, $link2 );

/** OUTPUT OF ABOVE TESTS
http://test.site.com/cat1/cat2/page-123.txt
http://test.site.com/cat1/cat2/cat3/page-1.txt
**/
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.