I have the following code:
$string = '[url]http://google.com[/url]';
$bbreplace = array ('/\[url\](.+?)\[\/url\]/');
$bbreplacements = array ('\\1');
$string = preg_replace($bbreplace, $bbreplacements, $string);
print $string;
Which creates a url called http://google.com/ that points to
mydomain.com/"http://google.com/"
instead of
http://google.com/
How can I fix this? Thanks
You don't need to escape the " inside '.
$bbreplacements = array ('\\1');
(BTW, use a BBcode parser.)
Related
I have string variable like ABC/DF/G I want extract DF from it means text between two /.
I have tried like below
$string= "ABC/DF/G";
$code = substr($string ,strpos($string,'/'));
echo $code;
but I am getting result like below
/DF/G
Let me know if someone can help me for do it.
Thanks!
You can use explode function to split a string into an array.
$string= "ABC/DF/G";
$code = explode('/', $string);
echo $code[1];
My string:
'KCP-PRO;first_name last_name;address;zipcode;country' //for example: 'KCP-PRO;Jon Doe;Box 564;02201;USA'
or
'KCP-SAT-PRO;first_name last_name;address;zipcode;country'
How can i change the first part (KCP-PRO or KCP-SAT-PRO) and change it to (KCP,PRO or KCP,SAT,PRO)? The outcome has to be:
'KCP,PRO;first_name last_name;address;zipcode;country'
or
'KCP,SAT,PRO;first_name last_name;address;zipcode;country'
I haven't tried the code myself but I guess this will do the trick
$string = 'KCP-SAT-PRO;first_name last_name;address;zipcode;country';
$stringExploded = explode(';', $string);
$stringExploded[0] = str_replace('-', ',', $stringExploded[0]);
$output = implode(';', $stringExploded);
//output should be KCP,SAT,PRO;first_name last_name;address;zipcode;country
Hope this helps :)
Or you can use preg_replace_callback function with the following regex
^[^;]*
So your code looks like as
echo preg_replace_callback("/^[^;]*/",function($m){
return str_replace("-",',',$m[0]);
},"KCP-SAT-PRO;first_name last_name;address;zipcode;country");
I'm a newbie with programming; now, I have some problems with PHP programing.
I want to extract the URL from the following string:
"success":true,"load":"http:\/\/8.88.88.8\/list\/si3diwoe\/123","Live":true
The desired string is
http://8.88.88.8/list/si3diwoe/123
Can anyone tell me how this work in code?
Thank you very much!
$string = '"success":true,"load":"http:\/\/8.88.88.8\/list\/si3diwoe\/123","Live":true';
// The JSON way
$array = json_decode('{' . $string . '}'); // Wrap in {} to make object
$url = $array->load;
echo "Using JSON : $url", PHP_EOL;
// The RegEx way
preg_match('/load":"(.*?)"/', $string, $matches);
$url = stripslashes($matches[1]);
echo "Using RegEx: $url", PHP_EOL;
Output:
Using JSON : http://8.88.88.8/list/si3diwoe/123
Using RegEx: http://8.88.88.8/list/si3diwoe/123
If it's a json string, you can use json_decode to assign it to a variable, say $arr, then stripslashes($arr['load']);
if it's not, you can use explode("," , $string), and assign it to a variable, say $arr again. Run explode again (explode(":", $arr[1])) and assigned it to another variable, say $ar, then do stripslashes on $ar['1'];
I’m trying to get the ID from this URL but it keeps coming back empty. Here is my code:
This is what the URLs look like on the website i'm looking on:
<b><a href="index.php?page=news&id=32662f87eb22a90d81b2362c6ff458a57643eff1"
This is my PHP
$pattern = "#<b><a href=\"index.php?page=news&id=(.*?)\"#i";
preg_match_all($pattern,$openSite,$match);
? and . are special characters. You need to add a \ before them.
$pattern = "#<b><a href=\"index\.php\?page=news&id=(.*?)\"#i";
$pattern = "#\<b\>\s*\<a\s+href\=\"index\.php\?page\=news\&\;id\=([^\"\&]*)#i";
You can use a simpler pattern: /id=(.*)$/
<?php
$openSite = '<b><a href="index.php?page=news&id=32662f87eb22a90d81b2362c6ff458a57643eff1"';
$pattern = "/id=(.*)$/";
preg_match_all($pattern,$openSite,$match);
print_r($match[1])
?>
OUTPUT
Array
(
[0] => 32662f87eb22a90d81b2362c6ff458a57643eff1"
)
I'm trying to split a php string in to parts the first one include the delimiter but the second one doesn't
$string = "abc==123";
What I want exactly is to get
$string['0'] = "abc==";
$string['1'] = "123";
Thanks for help
Simple enough
<?php
$string = explode("==", $string);
$string[0] .= "==";
?>
I believe you want the function strstr
http://us1.php.net/strstr
You can use PHP's explode function,
$data = "abc==123";
$result = explode("==", $data);
echo $result[0]. "==";
echo $result[1];