Nom Nom Posted December 23, 2009 Share Posted December 23, 2009 I was just wondering how would you add a string in a certain point of a string. It's for my web crawler. <?php //So like, say: $var2 = "http://domain.co.uk" $var[0] = "href=\"/folder/\"" /* How would I add $var2 to $var[0] but in the href attribute? I normally wouldn't know what $var[0] equals until it was outputted/* ?> Kinda hard to explain, if you need any more explaination, just ask. (: Quote Link to comment https://forums.phpfreaks.com/topic/186108-php-adding-inside-a-string/ Share on other sites More sharing options...
rajivgonsalves Posted December 23, 2009 Share Posted December 23, 2009 something like this <?php //So like, say: $var2 = "http://domain.co.uk"; $var[0] = 'href="/folder/"'; $var[0] = preg_replace('#href="(.*?)"#', "href=\"$var2$1\"", $var[0]); echo $var[0]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/186108-php-adding-inside-a-string/#findComment-982838 Share on other sites More sharing options...
Nom Nom Posted December 23, 2009 Author Share Posted December 23, 2009 No, it's like um, an array, more than one. Quote Link to comment https://forums.phpfreaks.com/topic/186108-php-adding-inside-a-string/#findComment-982841 Share on other sites More sharing options...
rajivgonsalves Posted December 23, 2009 Share Posted December 23, 2009 I don't get what you mean please elaborate a little further Quote Link to comment https://forums.phpfreaks.com/topic/186108-php-adding-inside-a-string/#findComment-982842 Share on other sites More sharing options...
Nom Nom Posted December 23, 2009 Author Share Posted December 23, 2009 It is an array, 'cos it's a web crawler. I don't know what any of the parts of the array is. I'm trying to add the domain, into the href so I can click on the link. 'tis hard to explain. =/ EDIT: well, it is like what you said, just how would I do it to the whole array? Quote Link to comment https://forums.phpfreaks.com/topic/186108-php-adding-inside-a-string/#findComment-982846 Share on other sites More sharing options...
rajivgonsalves Posted December 23, 2009 Share Posted December 23, 2009 this <?php //So like, say: $var2 = "http://domain.co.uk"; $var[0] = '<a href="/folder/">test1</a>'; $var[1] = '<a href="/folder/">test2</a>'; $var = preg_replace('#href="(.*?)"#', "href=\"$var2$1\"", $var); print_r($var); ?> Quote Link to comment https://forums.phpfreaks.com/topic/186108-php-adding-inside-a-string/#findComment-982848 Share on other sites More sharing options...
Nom Nom Posted December 23, 2009 Author Share Posted December 23, 2009 What is the $1 bit for? Quote Link to comment https://forums.phpfreaks.com/topic/186108-php-adding-inside-a-string/#findComment-982850 Share on other sites More sharing options...
rajivgonsalves Posted December 23, 2009 Share Posted December 23, 2009 $1 stands for the existing matched href in the expression since it could be anything. Quote Link to comment https://forums.phpfreaks.com/topic/186108-php-adding-inside-a-string/#findComment-982852 Share on other sites More sharing options...
Nom Nom Posted December 23, 2009 Author Share Posted December 23, 2009 Ah thanks. It's working now. (: Quote Link to comment https://forums.phpfreaks.com/topic/186108-php-adding-inside-a-string/#findComment-982854 Share on other sites More sharing options...
Brandon_R Posted December 23, 2009 Share Posted December 23, 2009 If im not mistaken, you want to convert relative to absolute urls? $newurl = str_replace('href="/', $url ,$var[0]); Quote Link to comment https://forums.phpfreaks.com/topic/186108-php-adding-inside-a-string/#findComment-982856 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.