-1

i am trying to replace inside body tag using PHP but every time i am getting different output than i expecting the one.

Try :

$homepage = "<head>https://www.example.com</head> <body>https://www.example.com</body>";
$homepage = substr($homepage, strpos($homepage, "<body>"));
$homepage = preg_replace("/https:\/\/(.?)+\.example\.com/", "https://www.example.net", $homepage);
echo $homepage;

Output :

<head>https://www.example.net</body>

The output i am looking for :

<head>https://www.example.com</head> <body>https://www.example.net</body>

I just want to change/replace the string inside tag.

2 Answers2

0
  1. Split head & body
  2. Replace string in body
  3. Join head & body

Your regex was problematic, I assumed you wanted to catch both https://www.example.com and https://example.com

Here is what you are looking for:

<?php

$homepage = '<head><link rel="stylesheet" type="text/css" href="https://www.example.com/whatever.css"></head><body>This link <a href="https://www.example.com">https://example.com</a> and this one <a href="https://www.example.com">https://www.example.com</a> will be replaced</body>';

$neck_pos = strpos($homepage, "<body");
$head = substr($homepage, 0, $neck_pos);
$body = substr($homepage, $neck_pos);


$body = preg_replace("/https:\/\/[w]*\.*example\.com/", "https://www.example.net", $body);

$homepage = $head . $body;

echo $homepage;
Kalacione
  • 16
  • 4
  • Body tag have a class which generate random. I am trying regex to match all the class `` to `` but it not working. `$neck_pos = strpos($homepage, '');` –  Jan 26 '22 at 17:49
  • I've edited the answer to track the position of ``. Pro tip: - Use [RegExr](https://regexr.com/) to test your regex - Check the [php documentation](https://www.php.net/manual/en/function.strpos) to respect expected parameters – Kalacione Jan 26 '22 at 18:10
  • Thanks for the update and suggestion. Is it possible to use `regex` with `strpos` and `substr` ? –  Jan 26 '22 at 18:13
  • and same if i want to make a changes to `` tag then i have to use like this `$head = preg_replace("/https:\/\/[w]*\.*example\.com/", "https://www.example.net", $head);` - I am right? –  Jan 26 '22 at 18:14
  • Most of your questions will find answer in the [php documentation](https://www.php.net/manual/en/function.strpos) and by experimenting by yourself. – Kalacione Jan 26 '22 at 18:16
  • Machines always do what they are told to do. It is our job as programmers to figure out what we ask for. Split your problems into small puzzle pieces and solve them one at a time. – Kalacione Jan 26 '22 at 18:24
0

Well, to reiterate what you were told in the comments: don't use regex on HTML/XML - ever. Always use a parser and (preferably) xpath to do the search. Look at the example below as a first step - you WILL need to read up on the whole subject. If the HTML/XML gets more complicated, so will the search:

$homepage = "<head>https://www.example.com</head> <body>https://www.example.com</body>";
$doc = new DOMDocument;
libxml_use_internal_errors(true);
$doc->loadHTML($homepage, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

$xpath = new DOMXPath($doc); 
$target= $xpath->query('//body');
$target[0]->nodeValue="https://www.example.net";
echo $doc->saveHTML();

Output:

<p>https://www.example.com <body>https://www.example.net</body></p>
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
  • In output `` is missing. i am looking for output like `https://www.example.com https://www.example.net` –  Jan 26 '22 at 17:53