I have this string (hundreds of them actually) containing URLs and I would like to update them.
Here's the old URL format
http://oldDomain/a/b/document.aspx?p1=v1&p2=NEEDED_VALUE&morePsHere=moreVsHere
and here's what I need them to look like after the update
http://newDomain/c/d/NEEDED_VALUE
Pretty much all I needed to do was to extract the value of p2 in the old URL and append it to http://newDomain/c/d/ to create the new URL.
I assumed the string I was going to get would look like this:
$s = "http://oldDomain/a/b/document.aspx?p1=v1&p2=001&morePsHere=moreVsHere,
http://oldDomain/a/b/document.aspx?p1=v1&p2=002&morePsHere=moreVsHere,
http://oldDomain/a/b/document.aspx?p1=v1&p2=003&morePsHere=moreVsHere"
and I was able to update it using the following:
$newURLStart = "http://newDomain/c/d/"
$newStr = $null
$s.Split(",") | ForEach {
if ($_.IndexOf("p2=") -ne 1)
{
$neededValue = $_.Substring($_.IndexOf("p2=")+3)
if ($neededValue.IndexOf("&") -ne -1)
{
$neededValue = $neededValue.Substring(0,$neededValue.IndexOf("&"))
}
$newStr = $newStr + ", " + $newURLStart + $neededValue
}
}
$newStr = $newStr.TrimStart(", ")
$s = $newStr
BUT, it turns out that the string I'm going to get isn't plaintext and would actually look something like:
$s = '<div class="someClass"><p>SomeText</p><ul>
<li><a href="http://oldDomain/a/b/document.aspx?p1=v1&p2=001&morePsHere=moreVsHere">LINK ONE</a></li>
<li><a href="http://oldDomain/a/b/document.aspx?p1=v1&p2=002&morePsHere=moreVsHere">LINK TWO</a></li>
<li><a href="http://oldDomain/a/b/document.aspx?p1=v1&p2=003&morePsHere=moreVsHere">LINK THREE</a></li>
</ul></div>'
This is a bit more complex than my comma-delimited expectations! I need help updating my script to accommodate the fact. I'm thinking regex might come into play here to grab the URLs inside the href but I'm pretty noob when it comes to that.