preg_replace internal link - php

how do I do when I want to preg_replace a href, but only if it's my own?
$a = 'href="http://mysite.com/?s=Bananas&lang=en"';
$host = 'http://mysite.com';
$a = preg_replace('#href="'.$host.'\/?[(s|p)]=([.*?])&lang=([.*?])"#e','href="index.php#$1\/$2\\lang\/$3"',$a);
//The result I want:
echo $a;
//Becomes href="http://mysite.com/#s/Bananas\\lang/en"
But what am I doing wrong?
This regex-syntax is very difficult...

<?php
$a = 'href="http://mysite.com/?s=Bananas&lang=en"';
$host = 'http://mysite.com';
echo preg_replace('#href="'.preg_quote($host).'/\?(s|p)=(.*?)&lang=(.*?)"#','href="'.$host.'/#$1/$2\\\\\lang/$3"',$a);
?>
This seems to work for me :)

Related

Loading an Array Elements with a PHP variable

Hello There I'm having a problem creating an array
<?php
//I'm actually grabbing the list from MySQl
//$list = '"02","03"';
$friends_list_array = array($list);
echo $friends_list_array[0];
?>
This is the Code !
But It Doesn't Work
Expected Result : 02
Output what i got from above code : "02","03"
Someone help please ?
Use php explode() function:-
<?php
$list = '"02","03"';
$friends_list_array = explode(",",$list);
echo $friends_list_array[0];
?>
Output:-https://eval.in/839531
If you want output strictly 02:-
<?php
$list = '"02","03"';
$friends_list_array = explode(",",$list);
echo trim($friends_list_array[0], '"');
?>
Output:-https://eval.in/839537
You can try also this way:-
<?php
$friends_list_array = array(
"02",
"03"
);
echo $friends_list_array[0];

I am using ucwords function in php but it doesnt work for the strings like "T.m.traders" or "R.j.kumar".?

String that has to be converted
$bar = 'R.m.traders';
$bar = ucwords(strtolower($bar));
output should be
R.M.Traders
You need to add delemeter . also called custom delimeter. You can check here. Anyway try this:
<?php
$bar = 'R.m.traders';
$bar = ucwords(strtolower($bar), "."); //After dot(.) next letter will be in Capital.
echo $bar;
?>
Just Try this Hope it helps
$bar = str_replace('.', ' ', $bar);
$bar = ucwords(strtolower($bar));
$bar = str_replace(' ', '.', trim($bar));
ucwords should be doing the first letter caps. R.m.traders is consider single word. If you give R m traders means you will get R M Traders
Alter way:
$bar = 'R.m.traders';
$bar_array = explode('.',$bar);
if(!empty($bar_array)){
$temp = array();
foreach($bar_array as $bar_arr){
$temp[] = ucwords(strtolower($bar_arr));
}
$bar_array = $temp;
}
$bar = implode('.',$bar_array);
echo $bar;

Function doesn't use variables defined above it

I have a variable named $url in the top of my file:
<?php
$url = "http://myurl.com";
Later in the same file, I have this code:
<?php
$url = "http://myurl.com";
[...]
function errorOut($error, $type = "info", $rel = "/")
{
echo $url;
}
?>
However, that doesn't work, because it says $url isn't a valid variable. I have to do this:
<?php
$url = "http://myurl.com";
[...]
function errorOut($error, $type = "info", $rel = "/")
{
$url = "http://myurl.com";
echo $url;
}
?>
This doesn't make any sense to me because it shouldn't be out of scope because it's a layer above the function. How do I make it use the earlier $url variable?
They are not in the same scope. You have to let PHP know you will be using that global locally. It is preferable to not use a global and instead pass it as a variable though.
<?php
$url = "http://myurl.com";
[...]
function errorOut($error, $type = "info", $rel = "/")
{
global $url;
echo $url;
}
?>
See Variable scope for more information.
I was talking to someone in an IRC about this (he posted on here too before I joined), he said I should use
define("URL", 'http://example.com');
And whenever I reference that variable I should use URL, not $url
by passing the $url in your function:
function errorOut($error, $type = "info", $rel = "/", $url) //<<< here
and also calling it:
errorOut('...','...','...',$url);
NON WORKING EXAMPLE AS SEEN IN YOUR ANSWER
$a = 'test1';
$b = 'test2';
define ('URL','one');
define ('URL', 'two');
test($a,$b);
function test ($a,$b){
echo $a;
echo $b;
echo URL;
}
Won't work, URL will stay at 'one' // Will only work if you never want to change URL

Variable inside regular expression php

I a stuck with regular expression and i need help.
So basically i want to do somethning like this:
$data = "hi";
$number = 4;
$reg = '/^[a-z"]{1,4}$/';
if(preg_match($reg,$data)) {
echo 'Match';
}else {
echo 'No match';
}
But i want to use variable
$reg = '/^[a-z"]{1, variable here }$/';
I have tried:
$reg = '/^[a-z"]{1, '. $number .'}$/';
$reg = "/^[a-z\"]{1, $number}$/";
But not getting right result.
Tnx for help
In the first example you have space where you shouldn't have one,
you have:
$reg = '/^[a-z"]{1, '. $number .'}$/';
your should have:
$reg = '/^[a-z"]{1,'. $number .'}$/';
then it works just fine
Update: You have same error in second example - thanks to AbraCadaver
Another way to use variables in regex is through the use of sprintf.
For example:
$nonWhiteSpace = "^\s";
$pattern = sprintf("/[%s]{1,10}/",$nonWhiteSpace);
var_dump($pattern); //gives you /[^\s]{1,10}/

Php enclosure as a variable?

A begginers question. I have this little code:
<?php
$content = 'hello there, hello!';
echo substr_count("$content","hello");
?>
How could i replace 'hello there, hello!' part with another php enclosure, like
<?php the_content(); ?>
for example. What is the correct way of doing this? Thanks!
<?php
echo substr_count(the_content(),"hello");
?>
This:
<?php
$a = 10;
?>
<?php
$b = 20;
?>
<?php
echo $a + $b;
?>
--output:--
30
is equivalent to:
<?php
$a = 10;
$b = 20;
echo $a + $b;
?>
If the_content does not return a meaningful value but rather echos things out, use output buffering:
ob_start();
the_content();
$captured_content = ob_get_clean();
echo substr_count($captured_content, "hello");
$content = the_content();
echo substr_count($content,"hello");
or
echo substr_count(the_content(),"hello");
It is really the basics, try to look at some tutorials ;)

Categories