Include php preg_replace in string - php

I am trying to include a php function within a string but my syntax is wrong. Could anyone help please?
$string = "<div id=\"apostnav\"><span class=\"calcir b-" . $string=get_field('br_category');
$string=preg_replace("/[^a-zA-Z]/", "", $string);
echo strtolower($string) . "></span></div>";

Try this:
$string=get_field('br_category');
$string=preg_replace("/[^a-zA-Z]/", "", $string);
$string = "<div id=\"apostnav\"><span class=\"calcir b-" . strtolower($string) . "\"></span></div>";
I moved up two lines of code andn removed an echo from the last one.
Here the code with few optimizations:
$string = get_field('br_category');
$string = preg_replace("/[^a-zA-Z]/", '', $string);
$stringLower = strtolower($string);
$string = "<div id='apostnav'><span class='calcir b-$stringLower'></span></div>";

You can use printf too in order to print your html string with the variable:
printf('<div id="apostnav"><span class="calcir b-%s"></span></div>', strtolower(preg_replace('/[^a-zA-Z]/', '', get_field('br_category'))));

Related

PHP: Can I remove ‪ #‎ from string

I need to remove ‪#‎ from string. I found this method:
$string = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string);
It doesn't work for the Thai language. I want to remove like this:
from
‪#‎Apple‬ ‪#‎ผลไม้‬
to
#Apple #ผลไม้
I can not understand why str_replace() did not work for you. This will do the job:
function cleanString($string) {
$search = array('‪', '‎', '‬');
$replace = array('', '', '');
return str_replace($search, $replace, $string);
}
$string = '‪#‎Apple‬ ‪#‎ผลไม้‬';
echo $string . "\n";
echo cleanString($string) . "\n";
Output is:
‪#‎Apple‬ ‪#‎ผลไม้‬
#Apple #ผลไม้
Working example can be found at http://sandbox.onlinephpfunctions.com/code/bbdbdf0758e5ea06faf32281021ae859b6d75a51

How to remove anything character after the part of link from string?

I have make a try like this:
$string = "localhost/product/-/123456-Ebook-Guitar";
echo $string = substr($string, 0, strpos(strrev($string), "-/(0-9+)")-13);
and the output work :
localhost/product/-/123456 cause this just for above link with 13 character after /-/123456
How to remove all? i try
$string = "localhost/product/-/123456-Ebook-Guitar";
echo $string = substr($string, 0, strpos(strrev($string), "-/(0-9+)")-(.*));
not work and error sintax.
and i try
$string = "localhost/product/-/123456-Ebook-Guitar";
echo $string = substr($string, 0, strpos(strrev($string), "-/(0-9+)")-999);
the output is empty..
Assume there are no number after localhost/product/-/123456, then I will just trim it with below
$string = "localhost/product/-/123456-Ebook-Guitar";
echo rtrim($string, "a..zA..Z-"); // localhost/product/-/123456
Another non-regex version, but require 5.3.0+
$str = "localhost/product/-/123456-Ebook-Guitar-1-pdf/";
echo dirname($str) . "/" . strstr(basename($str), "-", true); //localhost/product/-/123456
Heres a more flexibility way but involve in regex
$string = "localhost/product/-/123456-Ebook-Guitar";
echo preg_replace("/^([^?]*-\/\d+)([^?]*)/", "$1", $string);
// localhost/product/-/123456
$string = "localhost/product/-/123456-Ebook-Guitar-1-pdf/";
echo preg_replace("/^([^?]*-\/\d+)([^?]*)/", "$1", $string);
// localhost/product/-/123456
This should match capture everything up to the number and remove everything afterward
regex101: localhost/product/-/123456-Ebook-Guitar
regex101: localhost/product/-/123456-Ebook-Guitar-1-pdf/
Not a one-liner, but this will do the trick:
$string = "localhost/product/-/123456-Ebook-Guitar";
// explode by "/"
$array1 = explode('/', $string);
// take the last element
$last = array_pop($array1);
// explode by "-"
$array2 = explode('-', $last);
// and finally, concatenate only what we want
$result = implode('/', $array1) . '/' . $array2[0];
// $result ---> "localhost/product/-/123456"

PHP's preg_replace returns null when replacing underscores with white spaces

I have a webpage which includes a hyperlink as follows:
$name = "Hello World";
echo "<a href='page.php?name='". preg_replace(" ", "_", $name) "'> bla bla </a>"
This generates the following link successfully:
...page.php?name=Hello_World
in my page.php I try to reverse the operation:
if($_SERVER[REQUEST_METHOD] == "GET"){
$name = $_GET['name'];
//testing if the problem is with GET
echo $name;
//now here's the problem:
$string = preg_replace("_", " ", $name);
echo $string;
}
the $name echoes correctly but the $string is always null
I've tried all possible combinations like ~~ and // and [_] and \s and using $_GET directly like:
preg_replace("_", " ", $_GET['name']);
none of them worked.
This problem has burned most of my day.
Any help is appreciated.
preg_replace accepts a regular expression as it's first arguments. Neither " " nor "_" are valid regular expressions.
In this case you can use str_replace.
The preg_replace syntax is incorrect as pointed by #Halcyon, the following is correct:
$string = preg_replace('/_/', ' ', $name);
But for such a simple find/replace you can use str_replace instead:
$string = str_replace("_", " ", $name);
echo $string;
you can create a function like sefurl
function makesefurl($url=''){
$s=trim($url);
$tr = array('ş','Ş','ı','I','İ','ğ','Ğ','ü','Ü','ö','Ö','Ç','ç','(',')','/',':',',');
$eng = array('s','s','i','i','i','g','g','u','u','o','o','c','c','','','-','-','');
$s = str_replace($tr,$eng,$s);
$s = preg_replace('/&amp;amp;amp;amp;amp;amp;amp;amp;.+?;/', '', $s);
$s = preg_replace('/\s+/', '-', $s);
$s = preg_replace('|-+|', '-', $s);
$s = preg_replace('/#/', '', $s);
$s = str_replace('.', '.', $s);
$s = trim($s, '-');
$s = htmlspecialchars(strip_tags(urldecode(addslashes(stripslashes(stripslashes(trim(htmlspecialchars_decode($s))))))));
}
echo "<a href='page.php?name='". makesefurl($name) "'> bla bla </a>";
and than you can convert it to before makesefurl function all you need to create another functin like decoder or encoder html command

php replace paragraph to newline

how to replace <p>hello</p> <p>world</p> to hello<br />world <br />
I've tried searching on stack but there is no matched result.
You could do this by using str_replace() function.
For instance:
$string = "<p>hello</p> <p>world</p>";
$string = str_replace('<p>', '', $string);
$string = str_replace('</p>', '<br />' , $string);
I try this myself and get what I expected
$pattern = '/<p>(\w+)\<\/p>/';
$subject = '<p>hello</p><p>world</p>';
$replacement = '${1}<br/>';
$out = preg_replace($pattern, $replacement, $subject);
I just wonder which is better regex or str_replace
I wrote a better solution, hope everybody can see it helpful and maybe improve it
$pattern = '/<p(.*?)>((.*?)+)\<\/p>/';
$replacement = '${2}<br/>';
$subject = 'html string';
$out = preg_replace($pattern, $replacement, $subject);
Use this to prevent breaking the first and last <p></p> :
$string = str_replace($string, '</p><p>', '');
But if a space comes between the tags, it won't work.
<?php
$str = '<p>hello</p> <p>world</p>';
$replaceArr = array('<p>', '</p>', '</p> <p>');
$replacementArr = array('', '', '<br />');
$str = str_replace($replaceArr, $replacementArr, $str);
echo $str;
?>
Try above code.

PHP String Replacement Not Working Properly

Here is my PHP code,
$string = 'https://www.mydomain.lk/';
$wordlist = array("http://", "www.", "https://", "/");
foreach ($wordlist as &$word) {
$word = '/\b' . preg_quote($word, '/') . '\b/';
}
echo $string2 = preg_replace($wordlist, '', $string);
I want to remove last "/" from $string.
so i add the "/" to $wordlist array, but its not working.
can somebody help me to fix this.
Thanks.
It seems that for the most part you wish to extract the hostname:
$host = parse_url($url, PHP_URL_HOST);
Removing the leading www. can then be done separately.
preg_replace('/^www\./', '', $host);
You want to only replace / at the end of the string, so you need a $, like /$, but preg_quote would end up escaping the $.
The best way to remove a trailing / is using rtrim, like Sudhir suggested. Alternatively you could remove the preg_quote loop and just use regular expressions in your $wordlist:
$string = 'https://www.mydomain.lk/';
$wordlist = array("#https?://#", "#www\.#", "#/$#");
echo $string2 = preg_replace($wordlist, '', $string);
you could use rtrim():
$string = 'https://www.mydomain.lk/';
echo rtrim($string, '/'); //gives --> https://www.mydomain.lk
Try this
$url= 'https://www.mydomain.lk/';
echo $newurl = rtrim($url,"/");
Output like this format
https://www.mydomain.lk
Please try this:
$string = 'https://www.mydomain.lk/';
$uri = parse_url($string);
$domain = str_replace("www.", "", strtolower($uri['host']));
echo $domain;

Categories